开发者

Bash shell scripting variables

I have the following line in my shell sc开发者_StackOverflowript:

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

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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜