Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash assign default value

Tags:

bash

${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.

I thought I could use this feature to write ${LONG_VARIABLE_NAME:=hello} instead of the longer LONG_VARIABLE_NAME=${LONG_VARIABLE_NAME:-hello}, but now bash also tries to execute 'hello' and that gives a command not found. Any way to avoid that? Or will I have to stick to the latter? Can someone give an example where the assign default is actually useful?

like image 870
zedoo Avatar asked Dec 14 '10 09:12

zedoo


People also ask

What is $@ in bash?

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.

How do you assign a null value to a variable in UNIX shell script?

Use the Bash null command to assign a variable if not already set. With the shell parameters expansion, you can assign a default value to a variable if that variable wasn't set. For example, ${myVar:=myDefaultValue} would set the variable myVar to the default value myDefaultValue .

What is $$ variable in bash?

$$ is a Bash internal variable that contains the Process ID (PID) of the shell running your script. Sometimes the $$ variable gets confused with the variable $BASHPID that contains the PID of the current Bash shell.


2 Answers

Please look at http://www.tldp.org/LDP/abs/html/parameter-substitution.html for examples

${parameter-default}, ${parameter:-default} 

If parameter not set, use default. After the call, parameter is still not set.
Both forms are almost equivalent. The extra : makes a difference only when parameter has been declared, but is null.

unset EGGS echo 1 ${EGGS-spam}   # 1 spam echo 2 ${EGGS:-spam}  # 2 spam  EGGS= echo 3 ${EGGS-spam}   # 3 echo 4 ${EGGS:-spam}  # 4 spam  EGGS=cheese echo 5 ${EGGS-spam}   # 5 cheese echo 6 ${EGGS:-spam}  # 6 cheese 

${parameter=default}, ${parameter:=default} 

If parameter not set, set parameter value to default.
Both forms nearly equivalent. The : makes a difference only when parameter has been declared and is null

# sets variable without needing to reassign # colons suppress attempting to run the string unset EGGS : ${EGGS=spam} echo 1 $EGGS     # 1 spam unset EGGS : ${EGGS:=spam} echo 2 $EGGS     # 2 spam  EGGS= : ${EGGS=spam} echo 3 $EGGS     # 3        (set, but blank -> leaves alone) EGGS= : ${EGGS:=spam} echo 4 $EGGS     # 4 spam  EGGS=cheese : ${EGGS:=spam} echo 5 $EGGS     # 5 cheese EGGS=cheese : ${EGGS=spam} echo 6 $EGGS     # 6 cheese 

${parameter+alt_value}, ${parameter:+alt_value} 

If parameter set, use alt_value, else use null string. After the call, parameter value not changed.
Both forms nearly equivalent. The : makes a difference only when parameter has been declared and is null

unset EGGS echo 1 ${EGGS+spam}  # 1 echo 2 ${EGGS:+spam} # 2  EGGS= echo 3 ${EGGS+spam}  # 3 spam echo 4 ${EGGS:+spam} # 4  EGGS=cheese echo 5 ${EGGS+spam}  # 5 spam echo 6 ${EGGS:+spam} # 6 spam 
like image 29
Jonathan L Avatar answered Oct 13 '22 05:10

Jonathan L


Use a colon:

: ${A:=hello} 

The colon is a null command that does nothing and ignores its arguments. It is built into bash so a new process is not created.

like image 141
camh Avatar answered Oct 13 '22 06:10

camh