If Statement | Shell Scripting

If Statement | Shell Scripting

ยท

3 min read

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

If-else syntax

if condition
then
  statement
else
  statement
fi

If-elif syntax

if condition
then
  statement
elif condition
then 
  statement
else
  statement
fi
  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

GitHub, HackerRank