I was trying to write a Bash script that uses an if statement.
if[$CHOICE -eq 1]; The script was giving me errors until I gave a space before and after [ and before ] as shown below:
if [ $CHOICE -eq 1 ]; My question here is, why is the space around the square brackets so important in Bash?
if command; then commands_if_exit_code_is_0; else commands_if_exit_code_is_nonzero; fi;.commandcan be any command, and[is just one of them.$CHOICEis"foo", you are attempting to run the command["foo".[[obeys the same requirements; you need spaces on both sides, and the command needs to end with]]. The doubled version is somewhat more versatile and robust; see the link by @codeforester above for a more nuanced discussion.