How To Create and Run Bash Script on Debian 11.3

To Create And Run Bash Script On Debian 11.3

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.

Step 1 : Check whether the Debian OS version is installed by using the below command

linuxhelp@linuxhelp:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 11 (bullseye)
Release:	11
Codename:	bullseye

Step 2: Log in as root user by using the below command

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

Step 3: Create file named testscript.sh by using the below command

root@linuxhelp:~# touch testscript.sh

Step 4: Long listing the files by using the below command

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
-rwr-r--  1 root root    0 Sep 30 08:51 testscript.sh
-rw-r--r--  1 root root   17 Sep 27 12:51 .vimrc

Step 5: Assign Permission to test script.sh file by using the below command

root@linuxhelp:~# chmod 755 testscript.sh 

Step 6: Long listing the files by using the below command

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

Step 7: Editing the testscript.sh file by using the below command

root@linuxhelp:~# nano testscript.sh 
#! /bin/bash
echo “hello $LOGNAME
echo “welcome to the world of `hostname`
echo “the host name is `hostname`
echo “your present working directory is `pwd`
echo “thank you, $LOGNAME”

ping 127.0.0.1

Step 8: Run the testscript.sh by using the below command

root@linuxhelp:~#  sh.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

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to Create and Run Bash Script on Debian 11.3. Your feedback is much welcome.

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 the 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 has 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 the same?
A
Bash is the acronym for "Bourne Again Shell", which is also considered to be a subset of 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.