I do not get any printed result for that, but I don't understand why?

read -e -i "no" -p "Install? " result

if [ '$result' == 'yes' ]; then
  declare -a subs=('one' 'two')
  for sub in "${subs[@]}"
    echo "$sub"

2 Answers 2


You need to double quote $result rather than single quote, otherwise it won't expand.

[ '$result' == 'yes' ] will never evaluate to true because it is trying to compare the literal $result to the literal yes.

Additionally, as Kusalananda points out; the == operator is for bash test constructs [[ while the = operator is for standard (POSIX) test constructs [.

Therefore, you should change that line to: [ "$result" = 'yes' ]

Another good tool to know about is the set builtin and its -x switch which can be used to trace your scripts. If you add set -x to the top of your original script and run it you will see the following printed out:

+ read -e -i no -p 'Install? ' result
Install? yes
+ '[' '$result' == yes ']'

As you can see, it is trying to compare '$result' to 'yes'. When quoted correctly, you wouldn't see the variable $result but rather it's expansion like so:

+ read -e -i no -p 'Install? ' result
Install? yes
+ '[' yes == yes ']'
+ subs=('one' 'two')
+ declare -a subs
+ for sub in '"${subs[@]}"'
+ echo one
+ for sub in '"${subs[@]}"'
+ echo two

Whenever you are banging your head against the wall with a script you should turn on set -x and trace what it's doing and where it's going wrong.

  • 4
    Also, = within [ ... ], == within [[ ... ]]. [[ ... ]] is bash extended syntax. [ ... ] is standard test syntax (which uses = for string comparisons).
    – Kusalananda
    Oct 11, 2017 at 14:57
  • 4
    @Kusalananda, anyway, -p, -e, -i, declare are all bashisms as well. [[ ... ]] comes from ksh. All shells that support that [[...]] (including bash) also support == as an alternative to = in their builtin [ command. I'd still stick to = as opposed to == though. There's no assigment in [[...]] or [ so no need to disambiguate == vs =. Oct 11, 2017 at 17:07

Following @Jesse_b's answer.

Why do I need to double quote the variable?

The difference between single and double quotes:

Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !.

From man bash

You need to remember this well, it will save you the trouble of debugging for a while.

  • If you're ever writing 500 lines of shell script, it's almost 100% certain that you're using the wrong tool for the job, or that you need to learn shell scripting better. (The typical 200-line production shell script that I see condenses to about 30 lines when the correct utilities are used, e.g. find instead of some monstrous ls | xargs stat | variable assignment | complicated logic | something else pipeline.)
    – Wildcard
    Oct 11, 2017 at 20:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .