Tools
Tools: Linux Fundamentals - Part 7: Bash Scripting (If Statements)
2026-02-03
0 views
admin
If Statements ## If...Else Statements ## Nested if Statements If statements execute code base on a condition. If the condition is true, the code block will run. The condition is enclosed in square brackets [ ] and the statement ends with fi, which is if spelled backward, marking the end of the if block. Example: Basic If Statement. If...else statements provide a way to execute one block of code if a condition is true and another block if it is false. The else keyword introduces the alternative block, and the statement ends with fi. Example: If...Else Statement Nested if statements allow you to place an if statement inside another if statement, enabling more complex logic. Each if block must be closed with its own fi.
Example: Nested If Statement. Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse CODE_BLOCK:
num=15
if [ $num -gt 10 ]; then echo "Number is greater than 10"
fi Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
num=15
if [ $num -gt 10 ]; then echo "Number is greater than 10"
fi CODE_BLOCK:
num=15
if [ $num -gt 10 ]; then echo "Number is greater than 10"
fi CODE_BLOCK:
num=8
if [ $num -gt 10 ]; then echo "Number is greater than 10"
else echo "Number is 10 or less"
fi Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
num=8
if [ $num -gt 10 ]; then echo "Number is greater than 10"
else echo "Number is 10 or less"
fi CODE_BLOCK:
num=8
if [ $num -gt 10 ]; then echo "Number is greater than 10"
else echo "Number is 10 or less"
fi CODE_BLOCK:
num=5
if [ $num -gt 0 ]; then if [ $num -lt 10 ]; then echo "Number is between 1 and 9" fi
fi Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
num=5
if [ $num -gt 0 ]; then if [ $num -lt 10 ]; then echo "Number is between 1 and 9" fi
fi CODE_BLOCK:
num=5
if [ $num -gt 0 ]; then if [ $num -lt 10 ]; then echo "Number is between 1 and 9" fi
fi
how-totutorialguidedev.tolinuxbash