Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "plus colon" ("+:") mean in shell script expressions?

Tags:

syntax

bash

shell

What does this mean?

if ${ac_cv_lib_lept_pixCreate+:} false; then :
  $as_echo_n "(cached) " >&6
else
  ac_check_lib_save_LIBS=$LIBS

Looks like ac_cv_lib_lept_pixCreate is some variable, so what does +: mean?

Where to learn complete syntax of curly bracket expressions? What is the name of this syntax?

like image 492
Suzan Cioc Avatar asked Sep 30 '13 15:09

Suzan Cioc


People also ask

What is Colon in shell script?

Description. 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 $() mean in shell script?

$() – the command substitution. ${} – the parameter substitution/variable expansion.

What does $# in bash mean?

$# is the number of positional parameters passed to the script, shell, or shell function. This is because, while a shell function is running, the positional parameters are temporarily replaced with the arguments to the function. This lets functions accept and use their own positional parameters.

What is double colon in bash?

It's nothing, these colons are part of the command names apparently. You can verify yourself by creating and running a command with : in the name. The shell by default will autoescape them and its all perfectly legal. Follow this answer to receive notifications.


2 Answers

In the “plus colon” ${...+:} expression, only the + has special meaning in the shell. The colon is just a string value in this case, so we could write that snippet as ${...+":"}.

For convenience, let's pretend the variable is called var, and consider the expression:

if ${var+:} false; then ...

If the shell variable $var exists, the entire expression is replaced with :, if not, it returns an empty string.

Therefore the entire expression ${var+:} false becomes either : false (returning true) or false (returning false).

This comes down to a test for existence, which can be true even if the variable has no value assigned.

It is very cryptic, but as it happens, is one of the few tests for the existence of a variable that actually works in most, if not all, shells of Bourne descent.

Possible equivalents: (substitute any variable name here for var)

if [[ ${var+"is_set"} == is_set ]]; then ...

Or, probably more portable:

case ${var+"IS_SET"} in IS_SET) ...;; esac
like image 139
Henk Langeveld Avatar answered Oct 26 '22 09:10

Henk Langeveld


Shell Parameter Expansion documentation for bash is here. No mention of +:, though it does mention :+:

${parameter:+word}
If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

like image 29
nickgrim Avatar answered Oct 26 '22 07:10

nickgrim