Do not get output for shell script using if and for
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
one
+ for sub in '"${subs[@]}"'
+ echo two
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.
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.