0

I need to write if inside another if to be looks like :

if(condition)
{
    if(condition)
    {
        //do something
    }

    //do something
}

How to write that but in bash scripting language ?

4

2 Answers 2

3

Condition tests using the if/then construct may be nested. The net result is equivalent to using the && compound comparison operator.

a=3

if [ "$a" -gt 0 ]
then
  if [ "$a" -lt 5 ]
  then
    echo "The value of \"a\" lies somewhere between 0 and 5."
  fi
fi

# Same result as:

if [ "$a" -gt 0 ] && [ "$a" -lt 5 ]
then
  echo "The value of \"a\" lies somewhere between 0 and 5."
fi
Sign up to request clarification or add additional context in comments.

Comments

1

The structure is:

if [ condition ]; then
    if [ condition ]; then
    #Do something                                                                                                                                  
    elif [ condition ]; then
        #Do something                                                                                                                                  
    fi
else
    #Do something                                                                                                                                      
fi

You can also check conditions together with &&(AND) and with ||(OR) and many other things to know...

You can start from HERE and HERE

1 Comment

@Akari I updated the answer with two useful links :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.