The guide may be used and shared for educational, non-commercial, not-for-profit purposes. Users are free to modify and distribute content.
- We will create a file, first.sh (you can use any text editor)
- The first line will be #! /bin/sh (it tells the interpreter that it is a bash script).
- We will write a command ls(list the subfolders in the directory)
#! /bin/sh
ls
- Run the script by executing command ./first.sh
Suppose, you have to push a commit to your git repository. Instead of writing the commands one by one, we can write a shell script that automates the whole process. Create a file git.sh
#! /bin/sh
git add .
git commit -m $1
git push origin master
We will automate the process of pushing commit to the repo. $1 is a variable that will get the input that the user will provide. To run this script, execute ./git.sh ‘initial commit’. ‘initial commit’ will be placed at the $1.
Comments in shell scripts are followed by #.
# This is a comment.
Variables are declared using the =
MY_NAME='Morty' #No spaces
NAME="John"
echo $NAME
echo "$NAME"
echo "${NAME}!"
Variables are accessed using the $ sign.
String are assigned to a variable
NAME="John"
echo "Hi $NAME" #=> Hi John
echo 'Hi $NAME' #=> Hi $NAME
Echo is used to display the statement in the shell just like printf in C language.
MY_NAME='Morty'
echo "My name is $MY_NAME"
<Output>
# My name is Morty
Let’s see how an if else statement looks in bash scripting.
if [[condition]];
then
# write the then instructions here
else
# write the else instructions here
fi
if [[ -z "$string" ]]; then
echo "String is empty"
elif [[ -n "$string" ]]; then
echo "String is not empty"
fi
These are some of conditions
git commit && git push
git commit || echo "Commit failed"
Functions are group of statements that together perform a task.
get_name() {
echo "John"
}
echo "You are $(get_name)"
Loops are a sequence of instruction s that is continually repeated until a certain condition is reached
for i in /etc/rc.*; do
echo $i
done
while true; do
···
done
for i in {1..5}; do
echo "Welcome $i"
done
#With step size
for i in {5..50..5}; do
echo "Welcome $i"
done
myfunc() {
echo "hello $1"
}
# ------------------------------------
# Same as above (alternate syntax)
function myfunc() {
echo "hello $1"
}
myfunc "John"
myfunc() {
local myresult='some value'
echo $myresult
}
# ------------------------------------
result="$(myfunc)"
myfunc() {
return 1
}
# ------------------------------------
if myfunc; then
echo "success"
else
echo "failure"
fi
Note that [[ is actually a command/program that returns either 0 (true) or 1 (false). Any program that obeys the same logic (like all base utils, such as grep(1) or ping(1)) can be used as condition, see examples.
Condition | Function |
---|---|
[[ -z STRING ]] | Empty string |
[[ -n STRING ]] | Not empty string |
[[ STRING == STRING ]] | Not Equal |
[[ NUM -eq NUM ]] | Equal |
[[ NUM -ne NUM ]] | Not equal |
[[ NUM -lt NUM ]] | Less than |
[[ NUM -le NUM ]] | Less than or equal |
[[ NUM -gt NUM ]] | Greater than |
[[ NUM -ge NUM ]] | Greater than or equal |
[[ STRING =~ STRING ]] | Regexp |
(( NUM < NUM )) | Numeric conditions |
[[ -o noclobber ]] | If OPTIONNAME is enabled |
[[ ! EXPR ]] | Not |
[[ X ]] && [[ Y ]] | And |
[[ X ]] | | [[ Y ]] |
Or |
Condition | Function |
---|---|
[[ -e FILE ]] | Exists |
[[ -r FILE ]] | Readable |
[[ -h FILE ]] | Symlink |
[[ -d FILE ]] | Directory |
[[ -w FILE ]] | Writable |
[[ -s FILE ]] | Size is > 0 bytes |
[[ -f FILE ]] | File |
[[ -x FILE ]] | Executable |
[[ FILE1 -nt FILE2 ]] | 1 is more recent than 2 |
[[ FILE1 -ot FILE2 ]] | 2 is more recent than 1 |
[[ FILE1 -ef FILE2 ]] | Same files |