Difference between quoting variables in shell script "if" statements?

Ryan

What is the difference between these two Bash if-statements? e.g.

if [ "$FOO" = "true" ]; then

vs

if [ $FOO = "true" ]; then

What is the difference? It seems that both statements work the same.

200_success

If the value of $FOO is a single word that doesn't contain a wildcard character \[*?, then the two are identical.

If $FOO is unassigned, or empty, or more than one word (i.e., contains whitespace or $IFS), then the unquoted version is a syntax error. If it happens to be just the right sequence of words (such as 0 -eq 0 -o false), the result could be arbitrary. Therefore, it is good practice to always quote variables in shell scripts.

Incidentally, "true" does not need to be quoted.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the difference between $@ and $* in shell script?

Is there any difference in how variables are referenced in shell script?

Pass quoted arguments to shell script and maintain quoting

Difference between different ways of running shell script

What is the difference in usage between shell variables and environment variables?

Nested If Statements in Shell Script

What is the difference between these if statements?

Prolog difference between statements

Difference between [ and [[ in if statements

whats the difference between these statements

Difference between delete statements

What's the difference between: ". [script]" or "source [script]", "bash [script] or $SHELL [script]", and "./ [script]" or "[script]"?

How to use ANSI-C Quoting with Shell variables?

Binary operator expected - quoting question for shell script args

Difference between shell variables which are exported and those which are not in bash

What is the difference between double-quoting and not double-quoting an array in Bash?

Is there a difference between double quoting a variable or a whole line in echo

Difference between two files after average using shell script or awk

In Unix speak what is the difference between a shell script and an executable?

The difference between running a scala script directly vs through a scala shell

Difference between "&&" inside variable and outside variable in shell script

What is the difference between sh and ./ when invoking a shell script?

What's the difference between using quotation with parameters and not using in a shell script

A expect script with multiple shell statements

Transforming IF statements to FOR or WHILE (Shell Script)

shell script crashes during if statements?

Difference between if statements and elif statements in this context?

Difference between <script></script> and <% %>

Difference between two yield statements