# Simple condition
if condition
then
script
fi
# If/else condition
if condition
then
script
else
script
fi
# If/elif/else condition
if condition
then
script
elif condition
then
script
else
script
fi
Examples
script_name.sh
#!/usr/bin/env bash
declare -i number=5
if [[ $number -gt 10 ]]
then
echo "$number is greater than 10"
else
echo "$number is not greater than 10"
fi
if (($number>10)) #The same condition as above
then
echo "$number is greater than 10"
else
echo "$number is not greater than 10"
fi
if (($number>10))
then
echo "$number is greater than 10"
elif (($number>4))
then
echo "$number is greater than 4"
else
echo "$number is not greater than 10"
fi
# While condition
while condition
do
script
done
# Until condition
until condition
do
script
done
While loop
script_name.sh
#!/usr/bin/env bash
declare -i n=0
while ((n<10))
do
echo "n:$n"
((n++))
done
Until loop
script_name.sh
#!/usr/bin/env bash
declare -i n=0
until ((n==10))
do
echo "n:$n"
((n++))
done
for condition
do
script
done
Example
script_name.sh
#!/usr/bin/env bash
for i in 1 2 3 #For in list
do
echo "n:$i"
done
for i in {1..20} #For in range
do
echo "n:$i"
done
for (( i=1; i<=10; i++)) #Iteration
do
echo "n:$i"
done
declare -a fruits=("apple" "orange" "cherry")
for i in ${fruits[@]} #Iterates an array
do
echo $i
done
declare -A person
person[name]="Keiny"
person[country]="Colombia"
for i in "${!person[@]}" #Iterates an associated array
do
echo $i: "${person[$i]}"
done
for i in $(ls) #Iterates all files
do
echo "Found a file: $i"
done
script_name.sh
#!/usr/bin/env bash
animal="dog"
case $animal in
bird) echo "Avian";;
dog|puppy) echo "Canine";;
*) echo "No match";;
esac
Example with no arguments
script_name.sh
#!/usr/bin/env bash
greet(){
echo "Hi there"
}
echo "See me"
greet
Example with arguments
script_name.sh
#!/usr/bin/env bash
greet(){
echo "Hello $1"
}
echo "See me"
greet Keiny
#Listing things
see_list(){
echo "Here the list from the function $FUNCNAME"
i=1
for f in "$@"; do
echo $i: "$f"
((i++))
done
}
see_list /*
echo
see_list cat dog donkey ant
Write files with output redirection operators (> and >>)
echo "abc" > out.txt
overwrites the content ofout.txt
echo "abc" >> out.txt
appends to the end ofout.txt
Read from files with input redirection (<) and read command
while read line; do echo $line; done < in.txt
Example
script_name.sh
#!/usr/bin/env bash
for i in 1 2 3 4 5
do
echo "This is line $i" > my_output.txt
done
#Printing content from the previous file
while read f
do echo "I read a line and it says: $f"
done < my_output.txt
Example from linkedin
script_name.sh
# A fortune-telling game
echo -e "\t\t Welcome to the "
echo -e "\t\t🔮 \033[5mMYSTICAL SPHERE\033[0m 🔮"
echo
waitingnumber=$(( 0 + RANDOM % 3 ))
mysterynumber=$(( 1 + RANDOM % 10 ))
declare -a fortunes=(
"You are likely to achieve the outcome you seek."
"Today is not a good day to do that."
"While it might seem unlikely, your chances are good."
"If you believe you will be successful, that's half the battle."
"If you cared enough to ask, you care enough to make it happen."
"I think you already know the answer to that."
"Stop wondering and start doing!"
"Yes, sure, whatever, I'm busy."
"Next Thursday might be a better day to do that."
"Sure, but what will the neighbors think?"
)
case $waitingnumber in
0) sleep 1; echo "One moment please..."; sleep 1;;
1) sleep 1; echo "Your fortune will be along shortly..."; sleep 2;;
2) sleep 1; echo "Preparing your fate..."; sleep 1;;
3) sleep 1; echo "The veil of mystery is dark today..."; sleep 3;;
esac
echo
echo "${fortunes[mysterynumber]}"
echo