You need to learn Bash Scripting right now playlist - NetworkChuck (Video)
We start by creating a file with the .sh
extension. For example script.sh
. Then we write the script in it. For example:
Basic Script
#!/bin/bash
echo "Hello World"
We can run this by bash script.sh
or ./script.sh
. But the second will only work if the script is executable (permission to execute). We can make it executable by chmod +x script.sh
. Now we can run it by ./script.sh
.
The first line of a bash script is called the shebang. It tells the system which interpreter to use to run the script. The shebang for bash is #!/bin/bash
. The shebang for python is #!/usr/bin/env python
. It vary from the langauge to language.
It is placeholder for a value. Just like any other programming language. We use $ to access the variable. Eg: $NAME
. We can also use ${NAME}
. The braces are used to make sure that the variable is not mistaken for a command.
#!/bin/bash
# Variable Declaration
NAME="John"
# Variable Usage
echo "My name is $NAME"
echo "My name is ${NAME}"
NOTE: We can create variables by NAME="John"
through CLI, we can't use in the script beacuse it is not exported. We can export it by export NAME="John"
. Now we can use it in the script.
But here is one more catch. If we exit the terminal and open a new one, the variable will be gone. To make it permanent, we can add it to the .bashrc
file. It is a hidden file in the home directory. We can open it by vi ~/.bashrc
or any other editor. We can add the variable to the file. Eg: export NAME="John"
.
We can take input from the user using the read
command.
#!/bin/bash
echo "Enter your name: "
read NAME
echo "Hello $NAME, nice to meet you!"
We can pass arguments to the script. The arguments are stored in the $1
, $2
, $3
and so on. $0
is the name of the script.
Eg: bash script.sh arg1 arg2
#!/bin/bash
echo "First Argument: $1"
We can do arthemetic operations in bash. We use the (( ))
to do arthemetic operations.
```bash
#!/bin/bash
echo $(( 5 + 5 ))
+
- Addition-
- Subtraction*
- Multiplication/
- Division%
- Modulus
We can use the if
statement to check for a condition. The syntax is:
#!/bin/bash
if [ "$1" == "John" ]
then
echo "Hello John"
exit 1
elif [ "$1" == "Doe" ]
then
echo "Hello Doe"
else
echo "I don't know you"
fi
if [$1 == "hello"],then echo "Hello World", fi
==
- Equal to>
- Greater than<
- Less than>=
- Greater than or equal to<=
- Less than or equal to!=
- Not equal to-ge
- Greater than or equal to-le
- Less than or equal to
-a
- And-o
- Or!
- Not
We can use loops to repeat a set of commands. There are two types of loops in bash. for
and while
. The the body is enclosed in do
and done
.
#!/bin/bash
for i in 1 2 3 4 5
do
echo $i
done
#!/bin/bash
i=1
while [ $i -le 5 ]
do
echo $i
(( i++ ))
done
We can use break
and continue
in loops. break
will break the loop and continue
will skip the current iteration.
#!/bin/bash
for i in 1 2 3 4 5
do
if [ $i -eq 3 ]
then
continue
fi
echo $i
done
We can create functions in bash. The syntax is:
#!/bin/bash
function sayHello() {
echo "Hello World"
}
sayHello
exit 1
- Exit the script with an error (non-zero exit code).- $RANDOM gives a random number between 0 and 32767.
- $SHELL gives the path of the shell.
- $USER gives the username of the user.
- $HOSTNAME gives the hostname of the machine.
jq is a command-line JSON processor. It is used to parse JSON. It is used to extract data from JSON. It is used to transform JSON. It is used to generate JSON.
sudo apt install jq
Format in JSON.
echo '{"name": "John", "age": 30}' | jq
Will print out the the specified key.
echo '{"name": "John", "age": 30}' | jq '.name'