You need quotes around $THEME here:
if [ $THEME == '' ]
Otherwise, when you do not specify a topic, $THEME will not change to anything, and the shell will see this syntax error:
if [ == '' ]
With added quotes:
if [ "$THEME" == '' ]
extending the empty $THEME instead gives the correct comparison:
if [ "" == '' ]
This ability for runtime syntax errors can be surprising for those whose background is in more traditional programming languages, but command shells (at least those used in the Bourne tradition) are somewhat different from each other. In many contexts, shell parameters behave more like macros than variables; this behavior provides flexibility, but also creates traps for the reckless.
Since you noted this bash question, it is worth noting that the phrase is not executed as a result of expanding the parameter inside the "new" test syntax available in bash (and ksh / zsh), namely [[ ... ]] . So you can also do this:
if [[ $THEME == '' ]]
Places you can leave without quotes are listed here . But itβs a good habit to always indicate parameter expansions anyway, unless you explicitly want word breaks (and even then see if arrays will solve your problem instead).
It would be more idiomatic to use the -z test operator instead of equality with an empty string:
if [ -z "$THEME" ]
Technically, you don't need quotes in this simple case; [ -z ] is true. But if you have a more complex expression, the parser will be confused, so it's best to use quotation marks. Of course, [[ ... ]] does not require quotes here:
if [[ -z $THEME ]]
But [[ ... ]] not part of the POSIX standard; in this respect == does not exist. Therefore, if you care about strict compatibility with other POSIX shells, stick to the quotation mark and use either -z or one = .
Mark reed
source share