Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ${ARGUMENT+x} mean in Bash? [duplicate]

Tags:

bash

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?

like image 763
Alessandro C Avatar asked Oct 23 '17 14:10

Alessandro C


People also ask

What does X mean in bash?

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.

What is ${ in shell script?

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.

What does ${ 1 mean in bash script?

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

What is ${ 2 in bash?

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.


2 Answers

It means that if $ARGUMENT is set, it will be replaced by the string x

Let's try in a shell :

$ 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

like image 55
Gilles Quenot Avatar answered Sep 23 '22 01:09

Gilles Quenot


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
like image 22
chepner Avatar answered Sep 24 '22 01:09

chepner