1

I cannot figure out how to use a simple if/elif/else structure in bash. I cannot believe how something as trivial as that can be so unintuitive and difficult. I've already spent quite a bit of time fiddling around with that. What I want to do is something like that:

aaa="xxx"
if [[ $aaa -eq "bbb" ]]; then
   echo "bbb"
elif [[ $aaa -eq "ccc" ]]; then
   echo "ccc"
else
   echo "something else"
fi

I've tried it with a single [, with two [[, with ((, with == instead of -eq, I'm really not a Linux guy and very confused about the syntax, I've seen all kinds of different syntaxes regardind if conditionals. It always prints bbb, no matter what value aaa has. Can somebody please explain to me how to do this so that it works?

DanDan
  • 1,038
  • 4
  • 15
  • 28
  • The `if` structure itself looks fine. The problem is more the `[[ ... ]]` construct, which is not related to the `if`. Have a look at the bash man page. The section _Compound Commands_ explains both `(( ... ))` and `[[ .... ]]`. – user1934428 Aug 02 '21 at 14:31

2 Answers2

4

-eq is for numeric comparison only, for more info consider reading:
Shell equality operators (=, ==, -eq)


Also, consider quoting the variables:
When to wrap quotes around a shell variable?


  • Changed -eq to ==:
  • Quoted the variables
#!/bin/bash

aaa="xxx"
if [[ "$aaa" == "bbb" ]]; then
   echo "bbb"
elif [[ "$aaa" == "ccc" ]]; then
   echo "ccc"
else
   echo "something else"
fi

something else

Try it online!
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • 1
    Thank your!!! That was it! I've tried it with "==" before, but it didn't work, maybe I've tried something else at the same time and that's what broke it. – DanDan Aug 02 '21 at 13:12
  • `==` is new fangled. `=` is old school. – William Pursell Aug 02 '21 at 13:12
  • Glad it helped @DanDan! Please consider [upvoting](https://meta.stackexchange.com/questions/173399/how-can-i-upvote-answers-and-comments) or [accepting](https://meta.stackexchange.com/q/5234/179419) any answers that might have helped you. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. – 0stone0 Aug 02 '21 at 14:03
1

Use -eq for numeric comparisons, not for strings. Also, you are using quotes incorrectly. Use double quotes to prevent field splitting when you expand variables. IOW, they are needed around variables, but not around literal strings (unless the literal string contains whitespace or characters that would be interpreted by the shell such as a backtick or a $, etc.). And don't use a string of if/else when a case statement is more appropriate. Overall:

#!/bin/sh

aaa="$1"
if [ "$aaa" = bbb ]; then
        echo "bbb"
elif [ "$aaa" = ccc ]; then
        echo "ccc"
else
        echo "something else"
fi

case $aaa in
bbb) echo bbb;;
ccc) echo ccc;;
*) echo something else;;
esac

Regarding quotes: there is absolutely nothing wrong with using quotes as in if [ "$aaa" = "bbb" ]; or case "$aaa" in, but it is almost always a mistake to omit them as in if [ $aaa = "bbb" ] Omitting the quotes in case $aaa in or var=$aaa is allowed because field splitting does not happen in those cases, but it is certainly best practice to include the quotes in those cases. Generally, use quotes around varaibles. if [ $aaa = "bbb" ] is a huge source of potential bugs, and should be avoided.

William Pursell
  • 204,365
  • 48
  • 270
  • 300