read choose
[ "$choose" == "y" -o "$choose" == "Y" ] && echo "Yes" && exit 0
[ "$choose" == "n" -o "$choose" == "N" ] && echo "No" && exit 0
echo "Wrong Input" && exit 0
But when I execute
sh ./choose.sh
terminal prompt me that
[: 4: n: :Unexpected operator
[: 5: n: :Unexpected operator
Is there any mistake in my bash script?
Thanks!
marked as duplicate by tripleee bash
Users with the bash badge can single-handedly close questions as duplicates and reopen them as needed.
Dec 16 '15 at 6:24
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
–
–
–
There is no mistake in your bash script. But you are executing it with sh which has a less extensive syntax ;)
So, run bash ./choose.sh
instead :)
–
–
–
–
–
POSIX sh doesn't understand ==
for string equality, as that is a bash-ism. Use =
instead.
The other people saying that brackets aren't supported by sh are wrong, btw.
–
–
–
–
–
In fact the "[" square opening bracket is just an internal shell alias for the test command.
So you can say:
test -f "/bin/bash" && echo "This system has a bash shell"
[ -f "/bin/bash" ] && echo "This system has a bash shell"
... they are equivalent in either sh or bash. Note the requirement to have a closing "]" bracket on the "[" command but other than that "[" is the same as "test". "man test" is a good thing to read.
site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0
with attribution required.
rev 2019.3.8.33007