Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

if
script
shell
else

The if-else condition in shell script

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

If-else is a very important coding concept. Just like other programming languages, shell script uses if-else statements to solve problems.

If a condition is true, the algorithm performs a certain action; else, it performs something else.

svg viewer

Syntax

if [ condition ]
then
  trueAction....
else
  falseAction
fi

Code

The following code outputs “Yes” if the two numbers are equal and “No” if they are not.

var1=50
var2=60
if [ $var1 == $var2 ]
then
echo "YES"
else
echo "NO"
fi

RELATED TAGS

if
script
shell
else
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring