Open In App

Bash Scripting – Working of Bash Scripting

Last Updated : 30 Dec, 2022
Like Article

Bash Scripting is a crucial component of Linux’s process automation. You can write a series of commands in a file and then execute them using scripting. A Bash script is a plain text file with a set of commands inside of it. These commands are a combination of ones we typically type on the command line ourselves (like ls or cp, for instance) and others that we could type on the command line but typically wouldn’t. Any command that can be executed regularly on the command line can be put into a script and will execute exactly as intended. Likewise, whatever you can include in a script can also be executed regularly on the command line and will provide the same results. You can save time by not having to repeatedly type specific commands as a result. Daily duties can be completed effectively and even scheduled for automated completion. Additionally, you can configure some scripts to run at startup, such as those that display a specific message when a new session is started or modify the environment.

Bash Scripting and Bash Shell

checking bash version


Bash shell is a software application known as the shell that offers Linux command line management. The shell program has changed through the years to accommodate different options. Bash is powerful because it can streamline tasks that are challenging to complete effectively using a GUI. Keep in mind that not all servers offer a graphical user interface (GUI), thus it is best to learn how to use a command line interface (CLI).

When a shell is used interactively, it will show information, like username, host, and a $ when it is awaiting a command from the user. This is known as the shell prompt.

[username@host ~]$

If the shell is running as the root user, the prompt will end with the # symbol.

[root@host ~]#

It is possible to set up different users to use various shells. But the majority of users choose to continue using the current standard shell. The GNU Bourne-Again Shell is the standard shell for many Linux distributions (bash). Bourne Shell succeeds Bash (sh). The startup script used by the shell when it is first launched may be found in the .bashrc or .bash profile file and allows you to modify how the shell behaves.



You can use nano to edit the add your script in bashrc config to load in a terminal startup. 



We write the bash script which is work on the bash shell, so we can use another shell but we should switch to the bash shell instead for running the bash scripts. The startup script written in bashrc may face conflict with other shells or will not be loaded in another shell. 

Working of Bash Script

We have the idea of applications and processes in Linux. A program is a blob of binary data that is normally saved on your hard drive and consists of a set of instructions for the CPU as well as additional resources (such as graphics and sound files). When we say we are running a program, we are really executing a process – a duplicate of the program. This information and instructions are copied from the hard drive into RAM. A small amount of RAM is also set aside for the process to store variables (which will hold temporary working data) and a few flags to let the operating system control and monitor the process while it runs. The same software may be executed in memory in multiple instances as different processes. For instance, I might be performing the command cp in each of the two open terminals. There would be two cp processes running on the machine in this scenario. Once they have completed operating, the system eliminates them, and the program cp is no longer represented by any processes. A Bash process is running at the terminal to provide us with the Bash shell. When we start a bash script, it doesn’t actually run in that process; instead, it creates a new one inside which it can run. This will be demonstrated in the section on variables that follows, and its ramifications should be made obvious. However, you shouldn’t worry too much about this phenomenon in general.

Identify a Bash Script file

However, they can function just fine without the sh extension. When an edit the file header or first line generally starts with bash bang. Bash bang basically consists of the directory of the bash shell program to be used for script execution. The hash exclamation mark #! character sequence is referred to as the Shebang. It basically uses to set the path of the bash program. The bash bang looks like the following structure

#! /bin/bash


#! /usr/bin/bash

Create and Run a simple Bash Script

To create a bash script open the terminal and create a file with the .sh extension and include the bash bang path, after this write the command which you generally run in the shell.

To create a file you can use GUI or in the terminal using nano or Vim editor to write the scripts

$ nano


To execute the program you can the following command. 

$ bash
hello world


$ ./
hello world

If you directly create a file with the proper structure using nano or vim, by default the file will be executable and if you run the ls command, you can see the fill color will be green, it shows that the file is executable, but if you do not follow that, the file will not be executable by default, you can make the file executable to run using ./ operator.



$ chmod +x
$ ./
hello world


Bash Scripting is just like another programming language which is deal with OS and kernels, bash specifically deal with Linux OS and make the work process easier to manage by writing short instructions for a specific task, a Large number of a server runs on Linux, where we perform various CI/CD pipeline operation where we have to build various application and task operation where bash scripts just make it a smooth process and save lots of time and there are lot more utilities of bash script like we can write a script for setup a system, build an application template in Linux and lot more.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads