DEV Community

Cover image for If Statement | Shell Scripting
Rahul Mishra
Rahul Mishra

Posted on • Originally published at programmingport.hashnode.dev

If Statement | Shell Scripting

This is a multipart blog article series where I am going to explain the concepts of shell scripting and how to write a shell script in Linux, UNIX or Mac based systems. You can also follow this tutorial blog using windows but for that you have to install a bash from.

In this article we will see the syntax of if statement and different types of operators used for doing comparison in between two things using if condition.

If then syntax

if condition
then
  statement 
fi
Enter fullscreen mode Exit fullscreen mode

If-else syntax

if condition
then
  statement
else
  statement
fi
Enter fullscreen mode Exit fullscreen mode

If-elif syntax

if condition
then
  statement
elif condition
then 
  statement
else
  statement
fi
Enter fullscreen mode Exit fullscreen mode
  1. Integer comparison:
    • -eq: is equal to
    • -ne: is not equal to
    • -gt: is greater than
    • -ge: is greater than or equal to
    • -lt: is less than
    • -le: is less than or equal to
    • < : is less than
    • <= : is less than or equal to
    • > : is greater than
    • >= : is greater than or equal to
  2. String comparison:
    • = : is equal to
    • == : is equal to
    • != : is not equal to
    • < : is less than, in ASCII alphabetical order
    • > : is greater than, in ASCII alphabetical order
    • -z : string is null, that is has zero length

Practical code file for this article

So, this will give you a brief idea about if statements in shell scripting. Hope you liked it and learned something new from it.

If you have any doubt, question, quires related to this topic or just want to share something with me, then please feel free to contact me.

📱 Contact Me

Twitter
LinkedIn
Telegram
Instagram

📧 Write a mail

rahulmishra102000@gmail.com

🚀 Other links

GitHub
HackerRank

Top comments (0)