Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of colon operator in ": ${foo=value}"

Tags:

bash

I understand the colon operator in bash that acts like a null, and I know it's used in parameter expansion, as well as being used other ways, but can someone explain this:

: ${SOMETHING='value'} 

From experimentation I know that this sets the environment variable $SOMETHING to 'value' but why?

"Just because it does" is a valid answer but then please point me to the documentation for it (which I can't seem to find) or a proper name for this usage would be useful. I'm hoping there's a more enlightening explanation though.

like image 565
Peter Coulton Avatar asked Sep 16 '11 12:09

Peter Coulton


People also ask

What is the colon in PATH variable?

Specifies the search path for the executable files. Include a colon ( : ) separator between the path names on UNIX systems. (Use the semicolon ( ; ) separator between path names on Windows systems.) You can specify the search path in various ways.

What does colon operator do in C?

Colon operators perform element-by-element operations. The matrices above are said to be c-conformable; the c stands for colon. The matrices have the same number of rows and columns, or one or the other is a vector with the same number of rows or columns as the matrix, or one or the other is a scalar.

What is Colon in command?

The : (colon) command 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. This command simply yields an exit status of zero (success). This can be useful, for example, when you are evaluating shell expressions for their side effects.

What does colon mean in Unix?

column command in Linux is used to display the contents of a file in columns. The input may be taken from the standard input or from the file. This command basically breaks the input into multiple columns. Rows are filled before columns. Empty lines from the input are ignored unless the -e option is used.


2 Answers

The expression ${SOMETHING='value'} sets SOMETHING to value if it isn't already set. This is a useful operator to have in many situations. However, it also returns the assigned value, so if you simply executed

${SOMETHING='value'} 

then your shell would try to invoke the command value. This might or might not do something unwanted; at the least it would throw a message "value: command not found".

To avoid this you can use the no-op :, which evaluates its argument and then throws it away, rather than executing it. This is documented here.

like image 139
Kilian Foth Avatar answered Sep 18 '22 10:09

Kilian Foth


Explained here : http://tldp.org/LDP/abs/html/parameter-substitution.html

If parameter not set, set it to default.

Both forms nearly equivalent. The : makes a difference only when $parameter has been declared and is null, [1] as above.

echo ${var=abc}   # abc echo ${var=xyz}   # abc # $var had already been set to abc, so it did not change. 
like image 30
Arnaud F. Avatar answered Sep 21 '22 10:09

Arnaud F.