What does “plus colon” (“+:”) mean in shell script expressions?

前端 未结 4 1235
既然无缘
既然无缘 2020-11-28 13:25

What does this mean?

if ${ac_cv_lib_lept_pixCreate+:} false; then :
  $as_echo_n \"(cached) \" >&6
else
  ac_check_lib_save_LIBS=$LIBS
相关标签:
4条回答
  • 2020-11-28 14:02

    Shell Parameter Expansion documentation for bash is here. No mention of +:, though it does mention :+:

    ${parameter:+word}
    If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

    0 讨论(0)
  • 2020-11-28 14:12

    Simple examples will prove

    I check for presence of a parameter TEST, if present echo "Yes" else I echo "No"

    openvas:~$ ${TEST+:} false  &&  echo "yes" || echo "no"
    no
    openvas:~$ TEST=1
    openvas:~$ ${TEST+:} false  &&  echo "yes" || echo "no"
    yes
    openvas:~$ 
    

    If you see, the parameter TEST is evaluated and it is actually unset, so it returns false and exit the path and goes to the OR Once I set the same, and test again, it flows to the echo part (continued &&) since it returns true

    Refer: this and that

    0 讨论(0)
  • 2020-11-28 14:13

    In the “plus colon” ${...+:} expression, only the + has special meaning in the shell. The colon is just a string value in this case, so we could write that snippet as ${...+":"}.

    For convenience, let's pretend the variable is called var, and consider the expression:

    if ${var+:} false; then ...
    

    If the shell variable $var exists, the entire expression is replaced with :, if not, it returns an empty string.

    Therefore the entire expression ${var+:} false becomes either : false (returning true) or false (returning false).

    This comes down to a test for existence, which can be true even if the variable has no value assigned.

    It is very cryptic, but as it happens, is one of the few tests for the existence of a variable that actually works in most, if not all, shells of Bourne descent.

    Possible equivalents: (substitute any variable name here for var)

    if [[ ${var+"is_set"} == is_set ]]; then ...
    

    Or, probably more portable:

    case ${var+"IS_SET"} in IS_SET) ...;; esac
    
    0 讨论(0)
  • 2020-11-28 14:27

    To illustrate what has already been said:

    Unset variable (note the blank lines as a result of some echo commands):

    $ unset foo
    $ echo ${foo}
    
    $ echo ${foo:+:}
    
    $ echo ${foo+:}
    

    Null variable:

    $ foo=""
    $ echo ${foo}
    
    $ echo ${foo:+:}
    
    $ echo ${foo+:}
    :
    

    Non-null variable:

    $ foo="bar"
    $ echo ${foo}
    bar
    $ echo ${foo:+:}
    :
    $ echo ${foo+:}
    :
    
    0 讨论(0)
提交回复
热议问题