Bash Scripting
A Walkthrough room to teach you the basics of bash scripting
This room is the walkthrough for learning bash scripting which is one of the default terminal language in linux. Bash terminal can also be used in windows and any other operating systems and can be helpful for many usages and automations.
Task 1 Introduction
This section gives some basic information about bash scripts. Bash scripts can be given in the terminal by one of two ways. They are,
- Using bash directly in the terminal
- Writing a bash script file in
.shextension to automate the script
Considering bash is a turing complete language and so we can use this to do almost anything programmatic. One of the most useful resource for bash scripting is https://devhints.io/bash.
Task 2 Our first simple bash scripts
Prining text in terminal using bash
we use echo command to print something in bash terminal.
1
echo "Hello"
Commenting code or document in bash
To comment we use the # before the tergeted comment.
1
2
3
# This is a comment
# Printing hello
echo "Hello"
Make .sh script executable
To make shell script executable we need to make the change the mode executable by using chmod
1
2
3
4
# There is a file example.sh converting to executable
chmod +x example.sh
# running the program
./example.sh
Who is the user
To find who is the current user in terminal we use whoami or id in the bash terminal
1
whoami
1
id
Task 3 Variables
Defining and using variables
Variables in bash termianls uses a $ before the variable identifier or variable name. For example,
1
2
$h = "Hello world!"
echo $h
Would print Hello world! in the terminal.
Debugging
To debug we have to use set - or set + command. we can even use bash -x command. to run a bash script file in debug mode,
1
2
# the file is example.sh
bash -x ./example.sh
to debug code in the file but not the entire code
1
2
3
4
5
6
name="Abul"
age=100
set -x
age=$((age + 50))
echo "$name is $age years old"
set +x
Task 4 Parameters
Parameters are inputs given in the shell script command. This is usd in shell script to make variable input and outputs for the given parameter. The general usages of this is,
1
bash ./hello.sh sadman
which if hello.sh contains the following
1
echo "Hello $1"
the numbers in the dollar signs are the parameters passed in the shell script call. the ./hello.sh itself is the 0th parameter and the name is the parameter. if we want to know the number of parameter we use # symbol
1
echo "$#"
Task 5 Arrays
Arrays are the 0 indexed list of values in bash. In bash we define array like following,
1
cars=('honda' 'audi' 'bmw' 'tesla')
we use access each variable using ${var[]}
1
2
3
4
# Print the entire array
echo "${cars[@]}"
# Print one variable
echo "${cars[1]}"
To delete a variable we unset it
1
unset cars[3]
Task 6 Conditionals
there are three conditionals,
- Basic if statement
1 2 3 4
num=15 if [ $num -gt 10 ]; then echo "Number is greater than 10" fi
- If…else statement
1 2 3 4 5 6
num=8 if [ $num -gt 10 ]; then echo "Number is greater than 10" else echo "Number is 10 or less" fi
- If…elif…else statement
1 2 3 4 5 6 7 8
num=10 if [ $num -gt 10 ]; then echo "Number is greater than 10" elif [ $num -eq 10 ]; then echo "Number is exactly 10" else echo "Number is less than 10" fi
- Nested if statement
1 2 3 4 5 6
num=5 if [ $num -gt 0 ]; then if [ $num -lt 10 ]; then echo "Number is between 1 and 9" fi fi
Comparisons
The comparing symbols are generally not some symbols like other programming language in bash.
| bash symbol | equivalant or description |
|---|---|
| -eq | == |
| -ne | != |
| -lt | < |
| -gt | > |
| -le | <= |
| -ge | >= |
| -f | File exists |
| -w | File have write access |
| -r | File have read access |
| -d | is a directory |