Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of :- (colon dash) in bash

Tags:

syntax

bash

What is the meaning of this style in bash?

${PUBLIC_INTERFACE:-eth0} 

What is the purpose of :-?

like image 393
pahko Avatar asked Apr 30 '12 20:04

pahko


People also ask

What is Colon used for 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 :").

What is Dash D in Bash?

-d is a operator to test if the given directory exists or not. For example, I am having a only directory called /home/sureshkumar/test/. The directory variable contains the "/home/sureshkumar/test/" if [ -d $directory ]

What does dash n mean in Bash?

Then we have an “if” statement followed by the “-n” flag, which returns true if a string is not null. We have used this flag to test our string “name,” which is null. It means that the “if” condition will not be executed since the value of the “-n” flag will be false in this case.

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.


1 Answers

If $PUBLIC_INTERFACE exists and isn't null, return its value, otherwise return "eth0".

There are actually a few of these documented in the bash man page:

${parameter:-word} Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

${parameter:=word} Assign Default Values. If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.

${parameter:?word} Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

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

like image 123
Tim Pote Avatar answered Oct 03 '22 19:10

Tim Pote