When should you push variables into quotation marks under boolean conditions?

Are the following two boolean expressions the same?

if [ -n $1 ] ; then if [ -n "$1" ] ; then 

And these two

 if [ $? == 0 ] ; then if [ "$?" == 0 ] ; then 

If not, when should you put the variable in quotation marks?

+4
source share
3 answers

put variables in quotation marks when it is likely that this value may contain spaces or even NOT be a continuous string of characters. So

how is $? there should always be something between 0 and 255, you do not need to specify this, because it is the return value set after the return of each subprocess. It is not possible to break this down by assigning a direct row value, i.e.

 $?=Is of course wrong and should be ?=Bad value assigment 

because the name of the user variable must begin with [A-Za-z_], so do not do this; -)

If for $ 1, if the value is passed as

 myscript "arg1 with spaces" 

test

 if [ -n $1 ] ; then 

explode

but test

 if [ -n "$1" ] ; then 

will be successful.

Ihth

+8
source

Using the [ (yes, this is a command) command, you should almost always use quotation marks. Exceptions are so rare that I can't even get into them. For instance,

 set -- 'x -a -zb' if [ -n $1 ]; then echo foo; fi # … crickets if [ -n "$1" ]; then echo foo; fi foo 

Bash also has a keyword [[ (yes, this keyword), which is smart enough to know about extensions and avoid the traps of not citing. (But still, it’s generally safe to quote inside expressions [[ .)

Since the command [ corresponds to POSIX, and [[ is the basis, you decide when to use it.

As for your second example, if you are comparing something with a number, using == is a bagism. If you are writing for bash, use an arithmetic expression like

 if (( $? == 0 )); then … 

But if you are trying to run POSIX, write

 if [ "$?" = 0 ]; then … 

Often direct comparisons with $? are a red flag because a team’s success / failure comparison matches exactly if :

 if somecommand; then … 

better than

 somecommand if (( $? == 0 )); then … 

But if you need to use it, $? is something like an exception, because it is guaranteed that there will only ever be a single-byte unsigned integer, so it’s quite safe to use unquoted data, although this will not damage the quote.

 $ false; if [ $? = 0 ]; then echo t; else echo f; fi f $ true; if [ $? = 0 ]; then echo t; else echo f; fi t $ false; if [ "$?" = 0 ]; then echo t; else echo f; fi f $ true; if [ "$?" = 0 ]; then echo t; else echo f; fi t $ false; if [ $? == 0 ]; then echo t; else echo f; fi f $ true; if [ $? == 0 ]; then echo t; else echo f; fi t $ false; if [ "$?" == 0 ]; then echo t; else echo f; fi f $ true; if [ "$?" == 0 ]; then echo t; else echo f; fi t 
+8
source

They are not always the same. If the variable in question is empty or contains spaces, the [ (alias for test ) command may have too few or too many arguments.

In bash you can use the built-in condition construct [[ -n $1 ]] , which does not break the variable into words before evaluating the condition, which means that double quotes are not needed.

+4
source

All Articles