DEV Community

Ayush Gupta
Ayush Gupta

Posted on

Terminal vs Shell vs Bash vs Bash Scripting

Hi Everyone,

Let's try to understand some useful concepts in Linux OS

we will start with the terminal

Terminal -

we can consider, a terminal is a place or a GUI interface, where we can type our commands, and when we hit enter, we can see the output of the commands.

Now let's see, what happens when we hit enter after typing the command ?

you may be thinking, either the command will work and give us the desired output or it will fail and give us error right

but who is executing these commands ? and showing back the output in the terminal

In simple terms the commands are actually executing by the kernel

but its not like we type a command in terminal and hit enter and that command will reach kernel and kernel is able to execute it.

Shell -

It is actually the shell who is taking the commands from terminal and sending it the kernel for execution

Terminal -> Shell -> Kernel
Enter fullscreen mode Exit fullscreen mode

But what if a command, doesn't exist or there is syntax error in the command, will Shell still send the command to Kernel ?

In this case shell will search first, if command doesn't exist, it will not send the command to kernel for execution, we will simply get error as following -

bash: command_name: command not found
Enter fullscreen mode Exit fullscreen mode

Bash-

Now there are other types of Shells also, who are just reading the commands from Terminal and sending to Kernel for execution.

and the most common Shell is Bash

Bash Scripting -

as of now we are typing the commands one at a time and when we hit enter, Shell is reading the command from terminal and sending to Kernel for execution right ?

now lets consider an example -

suppose you have 10 commands to follow to install some package in a server and you have to follow all the commands in a sequence, one way is to do it manually, mean we type a command and hit enter, then we type second command and hit enter .. this way we need to type all the 10 commands and finally our package is installed in the server.

But now we have a roadblock, suppose you have 5 servers also, and in all the 5 servers, we have to install the same package, will we follow same steps again and again in all the 5 servers ?

To solve this problem, we will use Bash Scripting

Let's see how to create a script ?

we need put all the 10 commands which we were following manually in a file as following -

hello.sh

here "hello" is the name of the file, ".sh" is the file extension

#!/bin/bash

echo "Hello World"
Enter fullscreen mode Exit fullscreen mode

Now consider you have put all the 10 commands in .sh file, now all you need to do is, to run or execute this file using following way -

./hello.sh
Enter fullscreen mode Exit fullscreen mode

Now Shell will read this file line by line, and start sending the commands you have written in each line to kernel for execution, until it has done reading all the lines of the file

Now as you can see, we are not evening typing the commands in terminal also, we have just put all the commands in a file, Shell itself, now doing all the work automatically, now to solve the problem of installing the same package in all the 5 server, all we need to do is run the same script in all the 5 servers, that's all.

so using Bash Scripting we were able to automate the process of installing our packages in servers.

Top comments (0)