What a bash script is
Bash script is a box of commands that used to run multiple commands at a time.
The Shebang (#!/bin/bash)
This is the very first line of the script that determine the shell, and based on the it'll execute the commands.
Making a script executable
When you create a script file you didn't have execute permission on that script file so you need to make that executable.
chmod u+x filename.sh
Variables
You can create variable like this
name="Dharam"
Make sure in the whole line don't have space if you add space anywhere that is not variable, and you can use the variable using the $ sign.
echo $name
And variable names are case-sensitive so if you use capital or small letter both are different variables.
name="Dharam"
Name="Aadi"
if you want to use the variable with text you we have double quote or single quote
Double Quote
name="Dharam"
echo "Hello my name is $name"
## it print this **Hello my name is Dharam**
when you use double quote it mean the doller sign represent the variable.
Single Quote
name="Dharam"
echo 'Hello my name is $name'
## it print this "Hello my name is $name"
Single quote use as a literal.
Arithmetic
When you want to do arithmetic operation you need to use
$(( ))
Conditions
Age determine script
#!/bin/bash
age=18
if [ "$age" -gt 20 ]
then
echo "You are an adult"
fi
This is a very very simple conditions it only determine one condition. if you want to add multiple condition you need to use elif.
#!/bin/bash
marks=75
if [ "$marks" -ge 80 ]
then
echo "You got a first division"
elif [ "$marks" -ge 60 ]
then
echo "You got a second division"
elif [ "$marks" -ge 38 ]
then
echo "You got a third division"
else
echo "You are fail!"
fi
Condition Number comparisons
-ge, -gt, -le, -lt, -eq, -ne
case, esac and *) also use to for one value.
Loops
For Loop
#!/bin/bash
for num in {1..10}
do
echo $num
done
This will print 1 to 10. for loop used to a fixed range we know the range.
While Loop
#!/bin/bash
count=0
while [ $count -lt 10 ]
do
echo $count
count=$(( count + 1 ))
done
While loop used to we don't know when the condition fullfill.
Until Loop
#!/bin/bash
count=1
until [ $count -gt 10 ]
do
echo $count
count=$(( count + 1 ))
done
The count loop is opposite of the while loop, it stops when the condition become true.
Functions
#!/bin/bash
name="Dharam"
greet(){
echo "Hello My name is $name"
}
greet
The functions are used to do some certain tasks. and we can also pass the arguments to the functions:-
#!/bin/bash
greet(){
echo "Hello My name is $1"
echo "I'm learning the $2"
}
greet Dharam DevSecOps
The $1, $2 are the arguments value.
Error Handling (Production-Quality)
Remember we have two type of code one is 0 and second is except 0, 0 mean command run succeed properly if the exit code is not 0 it mean command failed.
Exit Code
if you want to see your last command exit code you can use the $? this'll shows the last command exit code
echo $?
&& And ||
&&
This used to if the first command is run with exit code 0 then run second one otherwise not.
cd /folder/not/exists && rm -rf *
if we didn't use the && and the command before && fails the second command also run, so we use the && to prevent second command to run if first command is fail.
||
This used to if the command is failed then run the second one.
cd /myfol || echo "Do not entered in the folder"
set -e
This is very important to use in bash script.
#!/bin/bash
set -e
echo "First Command"
cd /folder/not/exists
rm -rf *
The set -e stop the script if the any command failed. this is lifesaver.
set -u
Stop the script if the variable is undefined. use it like set -e.
set -o pipefail
set -o pipefail makes the whole pipe count as failed if any command in it fails (not just the last). So the failure shows up in the exit code instead of being hidden.
set -euo pipefail
You can use the all options together.
trap
This mean run the command if the script fail or succeed, it is very helpful when we want to clear something if you script fail or succeed. and also if you press ctrl c then also it run.
Top comments (0)