DEV Community

Cover image for Learn Bash Scripting With Me ๐Ÿš€ - Day 4
Babs
Babs

Posted on

Learn Bash Scripting With Me ๐Ÿš€ - Day 4

Day 4 โ€“ If and If else conditional statement

In case youโ€™d like to revisit or catch up on what we covered on Day Three, hereโ€™s the link:

https://dev.to/babsarena/learn-bash-scripting-with-me-day-3-4kib
Enter fullscreen mode Exit fullscreen mode

In Bash scripting, conditional statements are used to make decisions in your script โ€” i.e., execute certain commands only if a condition is true (or false).

The image above is a simple conditional script created, so I will be explaining the script below.

Explanation

  • Square brackets [ ] are used to define the condition.

  • Notice that there are spaces around the condition:

[ "$NAME" = "Babs" ]
Enter fullscreen mode Exit fullscreen mode
  • If the spaces are missing, Bash will treat it incorrectly (e.g., as variable assignment instead of comparison).

  • The semicolon ; after ] is important when writing the then statement. It tells Bash that one command (the condition defined in the square bracket) has ended and another (then) begins.

  • The then statement contains the commands to run when the condition is true.

  • Every if statement must be closed with fi, which marks the end of the statement.

โœจ Understanding Bash Conditional Statements In the Image

When creating the conditional statement to check if the variable is equal to "Babs", we spaced it because:

  • If we do not space it, it would mean that we are trying to change the variable for "NAME" which we already defined.

We also end the conditional statement after closing the square bracket with a semicolon ";" because:

  • We are joining two commands together.

  • What I mean by that is because we want the statement to do something based on the condition described, which is why the then statement was included in order to tell it to:

echo "welcome boss"
Enter fullscreen mode Exit fullscreen mode

And also note that:

  • We always close an if statement with fi, or else the script will fail.

  • fi denotes the end of the if statement.

The above is the output of the script

โœ… The if-else Statement

Now for the if/else

  • Create and run the above script and see the outcome.
  • By now you should be familiar with the echo and read command
  • So in the above script created, the echo displays the instruction for us to enter our username
  • The read command takes input from us and saves it as the variable NAME
  • So the if statement would check if the username is equal to Babs which if it is will then echo the information โ€œwelcome back bossโ€
  • And the else is to display something else if the name is not equal to Babs so it will echo "who are you" and then the fi is to signify the end of the if statement.

The above image is the output of the script when the name is equal to Babs

The above is the output when the name is something else other than Babs.

๐Ÿ Summary

  • Use [ ] to wrap conditions in Bash.

  • Always keep spaces around brackets and operators.

  • Use then to specify what should happen if the condition is true.

  • Use else to define an alternative action.

  • Close every conditional block with fi.

These basics of if and if-else lay the foundation for more advanced conditionals.

Top comments (0)