How to use if elif else in bash
Asked Answered
M

3

26

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 regarding 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?

Mercantile answered 2/8, 2021 at 13:5 Comment(2)
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 [[ .... ]].Pelisse
and a link to the manual says more than a thousand wordsVoyles
B
33

-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!
Brookbrooke answered 2/8, 2021 at 13:8 Comment(2)
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.Mercantile
== is new fangled. = is old school.Dubitation
D
2

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.

Dubitation answered 2/8, 2021 at 13:9 Comment(0)
V
0

I was trying to fetch a value from a object response like this:

result=$( jq '.data.result[0].value[1]' readingFromThisfile.out)

And the result was "0". For this case the below condition worked fine:

if [ $result = '"0"' ]; then
    ## True condition
else
    ## False condition
fi
Vocoid answered 7/5 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.