Is there any way to run multiple line as a single line command in bash script.

This is my multiple line bash script

if [ $id -ge 30 ]
echo Successful
echo $id
echo fail
echo $id

I want to run this multiple line as a single line so far i have tried this way

id=15;if [ $id -ge 30 ]&&then&&echo Successful&&echo $id&&else&&echo fail&&echo $id&&fi

but when I executed this command in terminal i got this error

bash: syntax error near unexpected token `then'

Any idea how to change multiple line script to a single line command


1 Answer 1


Firstly you could replace the && with ; which is equivalent to the line breaks in your multiple-line code. Secondly I don't think you need the && (or ;) after the then statement.

The difference between the && and ; line breaks is that && will run the next block of code if and only if the previous block has executed properly.

  • Like this right id=15; if [ $id -ge 30 ]; then echo Successful; echo $id; else echo fail; echo $id; fi thanks it worked
    – Eka
    May 6, 2015 at 6:33
  • @Eka exactly. Glad I could help.
    – Dai
    May 6, 2015 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.