Bash command starting with a colon with another colon before equal characters

I could not find documentation that would explain the syntax below. What does he do in a bash script? This is a test?

: ${foo:=bar}; export foo 
+6
source share
1 answer

Command : - this is a zero utility :

This utility should only expand the arguments of the command. It is used when a command is required, as in the case of then if command, but nothing should be done by the command.

Also Built-in Bourne Shells :

Do nothing but expand the arguments and perform the redirect. The return status is zero.

The syntax ${foo:=bar} is a special parameter extension :

 ${parameter:=[word]} 

Assign default values. If the parameter is not specified or null, the parameter extension (or an empty string if the word is omitted) is assigned to the parameter. In all cases, the final value of the parameter must be replaced. This way you can only assign variables, not positional parameters or special parameters.

Bash Reference Guide Record :

 ${parameter:=word} 

If the parameter is not specified or null, the word extension is assigned to the parameter. Then the parameter value is replaced. Therefore, you cannot assign positional parameters and special parameters.

So, on the command line of your question:

 : ${foo:=bar}; export foo 

There are two teams:

  • : ${foo:=bar}
  • export foo

The first of which extends the variable foo , and if it is empty or not set, assigns it the value bar .

The second one then exports the variable foo for sub-shells and other processes.

+5
source

Source: https://habr.com/ru/post/986827/


All Articles