I am learning Korn shell which is based on Bourne shell. Below is my really simple code.
read ab
if [ $ab = "a" || $ab = "A" ] ; then
echo hi
fi
For some reason ||
operator is giving me the error:
[: missing `]'
a: command not found
I am learning Korn shell which is based on Bourne shell. Below is my really simple code.
read ab
if [ $ab = "a" || $ab = "A" ] ; then
echo hi
fi
For some reason ||
operator is giving me the error:
[: missing `]'
a: command not found
The correct way to write your if
condition is:
read ab
if [ "$ab" = "a" ] || [ "$ab" = "A" ]; then
echo hi
fi
With [ ... ]
, it is essential to put the variables in double quotes. Otherwise, shell will fail with a syntax error if the variables expand to nothing or if their expansion contains spaces.
See also:
If you use ksh or a modern bash you can use the non-standard [[
... ]]
instead of [
... ]
.
This has two benefits:
||
inside [[
... ]]
This makes it safe and shorter to write
[[ $ab = a || $ab = A ]]