Introduction
Conditionals let a script choose different actions based on information it checks. This helps scripts make decisions like a person would.
if [ condition ]; then commands elif [ another_condition ]; then other_commands else default_commands fi
if [ -f myfile.txt ]; then echo "File exists" fi
if [ "$USER" = "admin" ]; then echo "Welcome, admin!" else echo "Access denied" fi
if [ "$age" -ge 18 ]; then echo "Adult" elif [ "$age" -ge 13 ]; then echo "Teenager" else echo "Child" fi
#!/bin/bash read -p "Enter a number: " num if [ "$num" -gt 0 ]; then echo "You entered a positive number." elif [ "$num" -lt 0 ]; then echo "You entered a negative number." else echo "You entered zero." fi
if statements in a bash script?if in scriptsif statements check conditions and decide which commands to run next.if.if statement in bash?if syntaxthen.#!/bin/bash num=5 if [ $num -gt 10 ]; then echo "Greater than 10" elif [ $num -eq 5 ]; then echo "Equals 5" else echo "Less than 5" fi
numnum is set to 5.if [ $value = 10 ]; then echo "Value is 10" fi
num. Which conditional structure correctly branches all cases?Pass the test and this pattern moves unknown