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


if [ $FOO = "true" ]; then

What is the different? Seems both of two statements also works.

migrated from serverfault.com Aug 14 '13 at 22:09

This question came from our site for system and network administrators.

  • In Bash, use double square brackets and you don't need the quotes and you get additional benefits. – Dennis Williamson Aug 14 '13 at 11:11
  • @DennisWilliamson Not quite, you do need quotes on the right-hand side of the (in)equality and pattern matching operators. – Gilles Aug 14 '13 at 22:41
  • @Gilles: Sometimes not. For literal strings containing spaces or for literal strings or variables that contain glob characters that you want to be taken literally, the right side must be quoted: a='foo bar'; [[ $a == "foo bar" ]]. However, a variable which does not contain glob characters need not be: [[ $a == $a ]]. Word expansion is not performed inside double square brackets. And for regex matching, the pattern on the right hand side must not be quoted or it will be taken as literal string: [[ $a =~ .*oo.*r ]] (the pattern should be in an un-quoted variable, however, instead ... – Dennis Williamson Aug 15 '13 at 1:14
  • of being incorporated literally as here). For glob-style pattern matching, the glob characters must not be quoted, whether literal or in a variable: [[ $a == foo* ]]. Can you provide any additional examples of a requirement for quoting besides my literal string example? – Dennis Williamson Aug 15 '13 at 1:14

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.


To illustrate what problems it might cause, here a few examples.

Let's say we have two variables as follow:

FOO="some value"
BAR="some value"

Now we have two variables holding exactly the same string/value. If we did some if statements to test the result, in your case:

if [ $FOO = "$BAR" ]; then echo "match"; else echo "no match"; fi

At this point you will get bash: [: too many arguments. The unquoted $FOO holds now three values, namely '[ , some , value'. [ test keyword doesn't know what to execute because it is expecting the first or second argument to be an operator.

When we quote "$FOO" we explicitly tell if to look at the right values where no word splitting takes place.

Another example:

my_file="A random file.txt"
  • doing rm $my_file means removing 'A' 'random' 'file.txt' which makes it three files.
  • doing rm "$my_file" will remove "A random file.txt" which makes one file.

Hope I've not confused you with these examples.


In this specific case there is no difference.

However, if $FOO contains a space or some special characters, you will have a problem.

In the "$FOO" case, it will use the variable in total, to make the match insulating you from the space problem.

However, if you use $FOO and there is a special case it will affect the if statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.