DEV Community

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

Posted on • Originally published at programmingport.hashnode.dev

2

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay