0
0
Bash Scriptingscripting~15 mins

if-then-else in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Basic Decision Making with if-then-else in Bash
📖 Scenario: You are writing a simple script to check if a number is positive, negative, or zero. This is like checking the weather and deciding what to wear: if it's hot, wear shorts; if cold, wear a jacket; else wear something in between.
🎯 Goal: Build a bash script that uses if-then-else statements to print whether a number is positive, negative, or zero.
📋 What You'll Learn
Create a variable number with the value 5
Create a variable zero with the value 0
Use if, elif, and else to check if number is greater than, less than, or equal to zero
Print exactly Positive, Negative, or Zero based on the check
💡 Why This Matters
🌍 Real World
Scripts often need to make decisions based on input values, like checking if a file exists or if a user is logged in.
💼 Career
Understanding if-then-else logic is essential for automating tasks, writing installation scripts, and managing system operations.
Progress0 / 4 steps
1
Set the number variable
Create a variable called number and set it to 5.
Bash Scripting
Need a hint?

Use variable=value syntax without spaces in bash.

2
Set the zero variable
Create a variable called zero and set it to 0.
Bash Scripting
Need a hint?

Remember no spaces around the equals sign.

3
Write the if-then-else logic
Write an if statement to check if number is greater than zero using [ $number -gt $zero ]. Then use elif to check if number is less than zero using [ $number -lt $zero ]. Use else for the case when number equals zero. Inside each block, use echo to print Positive, Negative, or Zero respectively.
Bash Scripting
Need a hint?

Use if [ condition ]; then ... elif [ condition ]; then ... else ... fi structure.

4
Run the script to see the output
Run the script and print the output. The output should be exactly Positive.
Bash Scripting
Need a hint?

Run the script in a terminal or use bash script.sh if saved in a file.