How to create and run a bash script on Ubuntu 21.04

To Create and Run Bash Script on Ubuntu 21.04

Introduction

Bash is an acronym for “Bourne-Again Shell”. It is also used as a command-line interpreter for both UNIX and Linux. The BASH script also contains a series of commands that will later be executed while running the script. The bash script should also be commenced as #!/bin/bash.

Installation Procedure:

Checking the Ubuntu OS version installed

linuxhelp@linuxhelp:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

Log in as root user

linuxhelp@linuxhelp:~$ sudo -s
[sudo] password for linuxhelp: 

Change to root directory

root@linuxhelp:/home/linuxhelp# cd /root/

Create file named testscript.sh

root@linuxhelp:~# touch testscript.sh

Assign Permissionto testscript.shfile

root@linuxhelp:~# chmod 755 testscript.sh 

Long listing the files

root@linuxhelp:~# ls -la
total 32
drwx------  4 root root 4096 Sep 30 08:51 .
drwxr-xr-x 20 root root 4096 Sep  8 16:50 ..
-rw-------  1 root root 1991 Sep 30 08:47 .bash_history
-rw-r--r--  1 root root 3106 Aug 15  2019 .bashrc
drwx------  2 root root 4096 Apr 20 16:20 .cache
-rw-r--r--  1 root root  161 Sep 16  2020 .profile
drwxr-xr-x  3 root root 4096 Sep  8 17:00 snap
-rwxr-xr-x  1 root root    0 Sep 30 08:51 testscript.sh
-rw-r--r--  1 root root   17 Sep 27 12:51 .vimrc

Editing the testscript.sh file

root@linuxhelp:~# vi testscript.sh 

Run the testscript.sh

root@linuxhelp:~# ./testscript.sh 
Bash script is started
PING 192.168.7.150 (192.168.7.150) 56(84) bytes of data.
64 bytes from 192.168.7.150: icmp_seq=1 ttl=63 time=0.633 ms
64 bytes from 192.168.7.150: icmp_seq=2 ttl=63 time=0.723 ms
64 bytes from 192.168.7.150: icmp_seq=3 ttl=63 time=0.699 ms
64 bytes from 192.168.7.150: icmp_seq=4 ttl=63 time=0.961 ms
64 bytes from 192.168.7.150: icmp_seq=5 ttl=63 time=0.712 ms

--- 192.168.7.150 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4080ms
rtt min/avg/max/mdev = 0.633/0.745/0.961/0.112 ms
total 36
drwx------  4 root root 4096 Sep 30 08:53 .
drwxr-xr-x 20 root root 4096 Sep  8 16:50 ..
-rw-------  1 root root 1991 Sep 30 08:47 .bash_history
-rw-r--r--  1 root root 3106 Aug 15  2019 .bashrc
drwx------  2 root root 4096 Apr 20 16:20 .cache
-rw-r--r--  1 root root  161 Sep 16  2020 .profile
drwxr-xr-x  3 root root 4096 Sep  8 17:00 snap
-rwxr-xr-x  1 root root  108 Sep 30 08:53 testscript.sh
-rw-r--r--  1 root root   17 Sep 27 12:51 .vimrc
Bash script completed

With this step creating and running the bash script file is completed

FAQ
Q
Can Python replace bash?
A
The answer is yes as using python provides various benefits and it can be also installed by default on all the major Linux distributions.
Q
What language is Linux terminal?
A
The language that is commonly used is a shell script that is sometimes referred to as “shebang”.
Shell scripts are usually implemented by interpreters present in the Linux kernel.
Q
What is the difference between C shell and bash?
A
CSH is a C shell while BASH stands for Bourne Again shell. The two are both Unix and Linux shells. whereas the C-shell commands start with '#' and for bash it is ‘;’. The CSH also its own set of features and the BASH unites the features of other shells.
Q
Is bash a terminal or shell?
A
Image result for bash vs shell
The GUI window refers to the terminal and it can also be seen on the screen as it requires commands and shows the output.
The bash is particularly a shell and the primary example for it is sh(bourne shell), csh(c shell), and tcsh(turbo c shell).
Q
Is shell scripting and bash scripting are the same?
A
Bash is the acronym for "Bourne Again Shell", which is also considered to be a subset of the shell scripting.
Shell scripting is scripting in any shell and eases the interaction between the user and operating system, while bash scripting can be done only for the bash.