I am trying to understand this piece of code:

. functions.sh || { : ; echo "Error while loading the specified file" >&2; exit 2; }

I get that the code in the bracket is called when the specified file isn't available. But what does this : ; mean? Moreover, when you delete it, then the script doesn't work.

share|improve this question
up vote 16 down vote accepted

The colon is null statement, so it does nothing. The semi-colon ends a list of commands.

Not sure why anyone would write the above, it's basically "do nothing, then do the echo" which seems like it could be simplified. Could be somebody's copy-paste baggage.

share|improve this answer
    
But if you write this: . functions.sh || {echo "Error while loading the specified file" >&2; exit 2; } then it doesn't work - the exit command somehow requires it I guess. – aa007 May 3 '13 at 11:40
1  
Nevermind .. I forgot to add a space after the { symbol. Thanks a lot! :) – aa007 May 3 '13 at 11:43
11  
do_something || : is an idiom meaning to ignore the failure of do_something which would end the entire script if set -e AKA set -o errexit was enabled. – Martin Vidner May 3 '13 at 12:12
2  
To add to Martin's comment, someone else probably didn't understand the cmd || : idiom, and rather than replace : with the log message, kept it inside a new command group. – chepner May 3 '13 at 12:19
1  
Autoconf uses : all over the place to reset the $? status which some shells screw up after an if block... Not to be recommended generally. (We still support the dreaded Solaris /bin/sh though!) – Nicholas Wilson May 3 '13 at 12:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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