Annie B
Annie B

Reputation: 647

Bash shell scripting variables

I have the following line in my shell script:

if [ -n "${USE_X:+1}" ]; then

I cannot figure out what the ":+1" part means. Any ideas?

Upvotes: 1

Views: 357

Answers (3)

jcomeau_ictx
jcomeau_ictx

Reputation: 38492

since aioobe already answered the question itself, here's a way to search a long manpage like Bash's using a regex, using this question as an example:


/\{.*:\+

The first forward-slash puts less (the manpage viewer) into search mode; the regex says to search for a left bracket, followed by any amount of stuff, then a colon followed by a plus sign. The bracket and plus need to be escaped because they have special meaning to the regex parser.

Upvotes: 2

aioobe
aioobe

Reputation: 421290

Have a look here. That url provides the following explanation:

${parameter:+alt_value}

If parameter set, use alt_value, else use null string.

and has the following example:

echo
echo "###### \${parameter:+alt_value} ########"
echo

a=${param4:+xyz}
echo "a = $a"      # a =

param5=
a=${param5:+xyz}
echo "a = $a"      # a =
# Different result from   a=${param5+xyz}

param6=123
a=${param6:+xyz}
echo "a = $a"      # a = xyz

Upvotes: 5

Andreas Wong
Andreas Wong

Reputation: 60594

basically if $USE_X is set, the statement is evaluated to 1, otherwise null. Probably similar to

if [ -z $USE_X ];
then
    echo 1
else
    echo ""
fi

from http://tldp.org/LDP/abs/html/parameter-substitution.html#PATTMATCHING :

${parameter+alt_value}, ${parameter:+alt_value}
If parameter set, use alt_value, else use null string.

Both forms nearly equivalent. The : makes a difference only when parameter has been declared and is null, see below.

Upvotes: 4

Related Questions