Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of colon in Bash after a double pipe

Tags:

bash

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.

like image 750
aa007 Avatar asked May 03 '13 11:05

aa007


People also ask

What does colon mean in Bash?

Bash and sh both use colons (":") in more than one context. You'll see it used as a separator ($PATH, for example), as a modifier (${n:="foo"}) and as a null operator ("while :"). You'll also see the null operator usage in "if-then" sections: if [ "$T1" = "$T2" ] then : else echo "Nope" fi.

What does double pipe mean in Bash?

A double pipe (||) causes the following command to be executed only if the preceding command returned an exit status of non-zero. $ cat bf5 grep $1 /usr/lib/spell/list || echo "not in dictionary" $ bf5 cochise not in dictionary. Test. The test command is used to check conditions in the Bourne Shell.

What does colon mean in command line?

It is used when a command is needed, as in the then condition of an if command, but nothing is to be done by the command.

What is the semi colon for in Bash?

When the shell sees a semicolon (;) on a command line, it's treated as a command separator -- basically like pressing the ENTER key to execute a command.


1 Answers

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.

like image 100
unwind Avatar answered Sep 22 '22 18:09

unwind