Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
745 views
in Technique[技术] by (71.8m points)

bash - What does "plus colon" ("+:") mean in shell script expressions?

What does this mean?

if ${ac_cv_lib_lept_pixCreate+:} false; then :
  $as_echo_n "(cached) " >&6
else
  ac_check_lib_save_LIBS=$LIBS

Looks like ac_cv_lib_lept_pixCreate is some variable, so what does +: mean?

Where to learn complete syntax of curly bracket expressions? What is the name of this syntax?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...