I have a bash script that uses the following syntax:
if [ ! -z ${ARGUMENT+x} ]; then
What is the meaning of the "+x" syntax after the argument name?
Bash Shell -x Option. Invoking a Bash shell with the -x option causes each shell command to be printed before it is executed. This is especially useful for diagnosing problems with installation shell scripts.
Here are all the ways in which variables are substituted in Shell: ${variable} This command substitutes the value of the variable. ${variable:-word} If a variable is null or if it is not set, word is substituted for variable.
- 1 means the first parameter passed to the function ( $1 or ${1} ) - # means the index of $1 , which, since $1 is an associative array, makes # the keys of $1. - * means the values of of the # keys in associate array $1.
It means "Use the second argument if the first is undefined or empty, else use the first". The form "${2-${1}}" (no ':') means "Use the second if the first is not defined (but if the first is defined as empty, use it)". Copy link CC BY-SA 2.5.
It means that if $ARGUMENT
is set, it will be replaced by the string x
$ echo ${ARGUMENT+x}
$ ARGUMENT=123
$ echo ${ARGUMENT+x}
x
You can write this with this form too :
${ARGUMENT:+x}
It have a special meaning with :
, it test that variable is empty or unset
Check bash parameter expansion
Rather than discussing the syntax, I'll point out what it is attempting to do: it is trying to deterimine if a variable ARGUMENT
is set to any value (empty or non-empty) or not. In bash
4.3 or later, one would use the -v
operator instead:
if [[ -v ARGUMENT ]]; then
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With