21 Dec 2011 @ 8:00 PM 

Tomorrow, we will talk about pipes (and file descriptors). But today, let’s talk about the other ways to chain commands.

One of the easy way for you to have to commands running one after the other, without you staying in front of the screen (see you at Starbucks), is to chain them using a semi-column :


Once you will have defined the commands “RunVeryLongProcess“, “PrintReportOnVeryLongProcess“, and “SendMailToBoss“, you will get a pay rise because the three jobs have been running one after the other, for 9 hours, and you have send a mail to your boss at 11pm ! How dedicated are you ! (At least to a Venti Macchiato :-)).

Yep, but what if RunVeryLongProcess fails ? Oops… You have send an empty report to your customer, and a mail to your boss saying that everything’s OK…

To avoid this, you can use the logic operators “and” (“&&”) and “or” (“||”), e.g.

RunVeryLongProcess && PrintReportOnVeryLongProcess && SendMailToBoss

Now, if “RunVeryLongProcess” or  “PrintReportOnVeryLongProcess“, no mail will be sent to your boss. You might not be the employee of the week, but you will still have a job 🙂

This “and” operator means that if the first job succeeds, the next one will be kicked. The keyword here is if, as you can do the same with an if statement, like this :


if [ $? -eq 0 ]



if [ $? -eq 0 ]





I’m tired only to write this, and it is only for three processes ! So, you can now make an extensive use of the && operator.

You can also use it to replace an if statement

&& operator instead of if statement.

Let’s say you want to check the variable “a”, if it is set to “morning” you say hello, and if not, you say “goodbye”…

(you can write the if, i’ll skip it, i’m tired)

And using && operator :

([ “$a” = “morning” ] && echo “Hello” ) || echo “goodbye”

You must understand how this is working. It starts by executing the first function, in this case, a test :

[ “$a” = “morning” ]

Then, if the result is true (return code 0), it tries the other part of the equation, i.e. :

echo “hello”

If the first part of the equation is wrong, then there is no need to execute the second part. The block around the && operand will be false. It then goes to the third part, which is supposed to be executed if the && equation is false, i.e.

echo “Goodbye”

If you want to use this, you must be sure that whatever function or command you call (except the first part of course) is going to return 0. Otherwise, you can get strange results, as both the “then” and “else” commands will be executed.

There is a way to avoid it, but it is getting complicated :

([ “$a” = “morning” ] && (say “hello” || true)) || say “goodbye”

By doing this,  even if the “say” function returns a non-zero status code, the “true” is executed. True always return “0” upon execution, so this is a way to avoid bizarre results.

This is the further you should go. Starting from this, there is no point to use the && operand anymore. Just write the if statement, and the guy fixing your script later will be thankful (and it might be you).

Regarding the order of execution, the parenthesis do not change it. The operations are executed from left to right.

(btw, the “say” command exists under Mac OS X and is quite fun).

That’s all for today. I hope you have enjoyed it, and see you tomorrow for a refresh on the pipes !


Nota bene

The reason why the parenthesis are working is that everything inside parentheses is in a subprocess, and the result is thus the one of the whole subprocess (the whole command, with both commands inside).

Therefore, you should not use this to pass a variable, as the subprocess is not going to affect the context of the main process !

Posted By: Dimi
Last Edit: 05 Jan 2012 @ 07:51 PM

Tags: , ,
Categories: Bash, Snippets


Responses to this post » (None)

Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


 Last 50 Posts
Change Theme...
  • Users » 67
  • Posts/Pages » 25
  • Comments » 4
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight