mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-10 00:36:48 +02:00
&& and || in Bash if statements.
This commit is contained in:
@@ -7,6 +7,7 @@ contributors:
|
|||||||
- ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
|
- ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
|
||||||
- ["Denis Arh", "https://github.com/darh"]
|
- ["Denis Arh", "https://github.com/darh"]
|
||||||
- ["akirahirose", "https://twitter.com/akirahirose"]
|
- ["akirahirose", "https://twitter.com/akirahirose"]
|
||||||
|
- ["Anton Strömkvist", "http://lutic.org/"]
|
||||||
filename: LearnBash.sh
|
filename: LearnBash.sh
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -81,6 +82,17 @@ fi
|
|||||||
echo "Always executed" || echo "Only executed if first command fails"
|
echo "Always executed" || echo "Only executed if first command fails"
|
||||||
echo "Always executed" && echo "Only executed if first command does NOT fail"
|
echo "Always executed" && echo "Only executed if first command does NOT fail"
|
||||||
|
|
||||||
|
# To use && and || with if statements, you need multiple pairs of square brackets:
|
||||||
|
if [ $NAME == "Steve" ] && [ $AGE -eq 15 ]
|
||||||
|
then
|
||||||
|
echo "This will run if $NAME is Steve AND $AGE is 15."
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $NAME == "Daniya" ] || [ $NAME == "Zach" ]
|
||||||
|
then
|
||||||
|
echo "This will run if $NAME is Daniya OR Zach."
|
||||||
|
fi
|
||||||
|
|
||||||
# Expressions are denoted with the following format:
|
# Expressions are denoted with the following format:
|
||||||
echo $(( 10 + 5 ))
|
echo $(( 10 + 5 ))
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user