Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script ending with a line containing only a colon?

I'm studying System V init scripts found in /etc/init.d/ in current Debian 7.4.0 wheezy release(But its also present in other, previous, releases). Almost all of them (from existing services) found in that folder end with, basically an empty line containing nothing but a colon (:) sign. Even the 'skeleton' template file that can be found there for the purpose of writing your own init scripts has this. Here is a copy/paste from the end of the code:

esac

:

(that was the end of an case statement and then there is the end of the file)

What is also interesting is that there is no exit 0 or exit $? call, except only in some conditions in the case statement, that would be called otherwise so it seems as if that colon sign is a sort of a replacement for it? Full skeleton file code is here: https://gist.github.com/ivankovacevic/9917139

What could that colon be and why?

like image 524
Ivan Kovacevic Avatar asked Apr 01 '14 14:04

Ivan Kovacevic


People also ask

What is $@ and $* in shell script?

• $* - It stores complete set of positional parameter in a single string. • $@ - Quoted string treated as separate arguments. • $? - exit status of command.

What is $() called in bash?

It turns out, $() is called a command substitution. The command in between $() or backticks (“) is run and the output replaces $() . It can also be described as executing a command inside of another command.

What is $@ in bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

What does Backquote mean in shell?

The back quote is the one to use when you want to assign output from system commands to variables. It tells the shell to take whatever is between the back quotes as a system command and execute its output. Using these methods you can then substitute the output into a variable.


2 Answers

The colon is a syntactic element that, essentially, does nothing, but returns true. It can be used whereever a command can be used.

It is sometimes needed where sh(1) requires a statement. E.g., this gives an error:

if [ "$a" = "" ] ; then
  # comment out this part for now
  # echo yes
else
  echo no
fi

bash: syntax error near unexpected token `else'

Replacing the comment with a : makes it work:

if [ "$a" = "" ] ; then
  # comment out this part for now
  : echo yes
else
  echo no
fi

It is rarely needed to explicitly use "exit 0" in the shell; in the absence of an exit statement, the shell script exits with the status of the last command, a shell script that just executes

/bin/false

will give an exit status 1:

$ echo $?
1

The colon is largely black magic, and I learned what little I know about it from experimentation.

like image 173
BertD Avatar answered Sep 22 '22 17:09

BertD


The : returns true status in BASH and is just a replacement for the word true. I don't see any benefit to using : as apposed to exit 0 at the end of a system init script other than to save characters or make it less readable.

As chepner point's out, : would ensure a true status without exiting the shell if the script is sourced.

It is also commonly used to replace logical negation using ! in statements.

if [[ $var ]]; then
    :
else
    echo no
fi

Is the same as:

if ! [[ $var ]]; then
    echo no
fi
like image 32
John B Avatar answered Sep 20 '22 17:09

John B