# Shell Scripting

Hi Everyone, welcome to **Day 04** of the **#90DaysofDevops** learning journey. In this, article we will cover Shell Scripting, and what is difference between `#!/bin/bash` and `#!/bin/sh`. How to write the first shell scripting Program. Let's get started...

### 🔸 Shell Scripting

Shell scripting is an important part of process automation in Linux. You can write a series of commands in a file and then execute them using scripting.

When a shell is used interactively, it displays a `$` when it is waiting for a command from the **<mark>user</mark>**. This is called the shell prompt.

`[username@host ~]$`

If the shell is running as **<mark>root</mark>**, the prompt is changed to `#`. The superuser shell prompt looks like this:

`[root@host ~]#`

A shell script involves the following basic elements

* Shell Keywords – if, else, break, etc.
    
* Shell commands – cd, ls, echo, pwd, touch, etc.
    
* Functions Control flow – if..then..else, case and shell loops, etc.
    

🔴**The file extension of** `sh`**.**

By naming conventions, bash scripts end with a `.sh`. However, bash scripts can run perfectly fine without the `sh` extension.

Scripts are also identified with a `shebang`. Shebang is a combination of `bash #` and `bang !`  followed the bash shell path. This is the first line of the script. Shebang tells the shell to execute it via bash shell. Shebang is simply an absolute path to the bash interpreter.

Below is an example of the shebang statement.

```plaintext
#! /bin/bash
```

**🔴Execution rights**

Scripts have execution rights for the user executing them.

An execution right is represented by `x`. In the example, a user has the `rwx` (read, write, execute) rights for the file `devops_script.sh`

```plaintext
chmod -x devops_script.sh
```

**🔴File color**

Executable scripts appear in a different color from the rest of the files and folders.

In my case, the scripts with execution rights appear in green.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679766059839/2c86e69c-b2cf-4106-a033-48fb608ce153.png align="left")

### 🔸 `#!/bin/bash` and `#!/bin/sh`

`/bin/sh` and `/bin/bash` are both Unix/Linux shell interpreters. `bash` and `share` two different shells.

**Bin/sh**

In Linux, users use the programming language called “sh” to code into the shell. “Sh” is also known as “Bourne Shell,” It was developed to be used with UNIX systems defined using the POSIX standards.

As shell follows the POSIX standards, a script written in Shell can be used in every Linux system without any problems. The same can’t be said for Bash. As Bash added better extensions and features, it slowly went away from the POSIX standards, thus affecting its portability.

**Bin/bash**

“Bash” is similarly a shell programming language. It is also known as “Bourne Again Shell”. Today, Bash is the default programming language for the shells in Linux. Bash has the same abilities as Shell, but it developed other functions and better extensions over time.

Bash can be called an implementation of Shell developed for the GNU project. As mentioned before, it includes all the shell features, but it developed better extensions. Hence, it can also be called a superset of Shell, including ideas drawn from other shells such as the C shell.

We choose Bash as the superior shell language since it is a more recent and enhanced version of Shell. Bash vs Shell has several strong features and advantages. So, it is simple to choose Bash over Shell while writing our Shell scripts.

GNU Bash: `#!/bin/bash`; POSIX shell: `#!/bin/sh`

### 🔸 Create and run your first shell script

Let’s first create a new directory named **script\_program** that will host all our bash scripts.

```plaintext
mkdir script_program
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679765396565/1cb2ec31-f285-40c7-83e8-219419104212.png align="left")

Now inside this **script\_program** directory', create a new file named **devops\_script.sh** using the cat command

```plaintext
cat > devops_script.sh
```

insert the following line in it by typing it in the terminal:

```plaintext
echo "I will complete the #90DaysofDevOps challenge"
```

Press **Ctrl+d** to save the text to the file and come out of the cat command.

You can also use a terminal-based text editor like 'vim' or 'nano'. If you are using a desktop Linux. Also, you can use **visual studio code.**

So, you are using the echo command to print "**I will complete the #90DaysOofDevOps challenge**". You can use this command in the terminal directly but in this task, you'll run this command through a shell script.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679766546244/43dd4871-a0ce-420a-a530-01cc1df702bf.png align="center")

Now make the file **devops\_script.sh** executable by using the chmod command as follows

```plaintext
chmod -x devops_script.sh
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679766690406/88d3e7c4-1a41-437e-bc9d-f5b8e5b578f4.png align="center")

Finally, execute your first shell script by adding your preferred shell "bash" before **devops\_script.sh**:

You can run the script in the following ways:

`./devops_script.sh` or `bash devops_script.sh`

```plaintext
bash devops_script.sh
```

You'll see **"I will complete the #90DaysOofDevOps challenge"** printed on the screen.

Here's a screenshot of all the steps you saw above:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679766486179/096e84f5-3631-4198-8fc4-64784bc18057.png align="center")

observation, after executing the script with the help of `bash devops_script.sh` name green.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679766059839/2c86e69c-b2cf-4106-a033-48fb608ce153.png align="center")

### **Task 1: Write a Shell Script to take user input, input from arguments, and print the variables.**

In bash, we can take user input using the `read` command.

```plaintext
#!/bin/bash
echo "Enter your First Name: "
read first_name
echo "Hello $first_name"

read -p "Enter last_name: "last_name
echo "Your last name is $last_name"
```

To prompt the user with a custom message, use the `-p` flag.

`read -p "Enter your age" variable_name`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679770838828/4beae93e-8adf-40aa-8bdc-89176fba8192.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679770775255/208eaa38-1561-4c8a-8d3a-c07342748e78.png align="center")

### Task 2: Write an Example of If else in Shell Scripting by comparing 2 numbers.

```plaintext
#!/bin/bash

read -p "Enter first number= " num1
read -p "Enter Second number= " num2

if [ $num1 -eq $num2 ]; then
        echo "Both numbers are equal."
elif [ $num1 -gt $num2 ]; then
        echo "num1 is greater than num2."
else
        echo "num1 is lesser than num2."
fi
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679772461357/512dd885-8bdf-499e-95fc-c6baac11fe2e.png align="center")

### **🎉 Task Done👍 Day 04 of #90daysofdevops 💯**

In this article, we have covered the Day 04 task of how to take input from users and with the help of If else in Shell Scripting compared two numbers.

***If you enjoyed this article please like it and share it with your friends and colleagues!***

**Thank you for reading🤓**
