How to create a Shell and Subshell on Rocky Linux 9.2

To create a Shell and Subshell on Rocky Linux 9.2

Introduction:

Shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of a shell, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions.

Subshell is a child shell that is spawned by the main shell (also known as the parent shell). It is a separate process with its own set of variables and command history, and allows you to execute commands and perform operations within a separate environment.

Procedure:

Step 1: Check the OS version by using the below command.

**[root@Linuxhelp ~]# cat /etc/os-release**
NAME="Rocky Linux"
VERSION="9.2 (Blue Onyx)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.2"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Rocky Linux 9.2 (Blue Onyx)"
ANSI_COLOR="0;32"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:rocky:rocky:9::baseos"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
SUPPORT_END="2032-05-31"
ROCKY_SUPPORT_PRODUCT="Rocky-Linux-9"
ROCKY_SUPPORT_PRODUCT_VERSION="9.2"
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.2"

Step 2: Create a script file named testscript.sh by using the below command.

[root@Linuxhelp ~]# touch testscript.sh

Step 3: Long list the files by using the below command.

[root@Linuxhelp ~]# ll
total 4
-rw-------. 1 root root 1039 Aug 13 22:24 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Desktop
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Documents
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Downloads
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Music
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Pictures
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Public
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Templates
-rw-r--r--. 1 root root    0 Sep 15 20:42 testscript.sh
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Videos

Step 4: Give executable permission to testscript.sh file by using the below command.

[root@Linuxhelp ~]# chmod +x testscript.sh

Step 5: Open testscript.sh file and Add the following Lines by using the below command.

[root@Linuxhelp ~]# vim testscript.sh
# !/bin/bash

echo script has begin

ping -c 10 192.168.6.130

cat testscript.sh

echo script has been ended......

Step 6: Execute the following script to ping the IP address 10 times and view the testscript.sh file by using the below command.

[root@Linuxhelp ~]# ./testscript.sh
script has begin
PING 192.168.6.130 (192.168.6.130) 56(84) bytes of data.
64 bytes from 192.168.6.130: icmp_seq=1 ttl=64 time=0.129 ms
64 bytes from 192.168.6.130: icmp_seq=2 ttl=64 time=0.646 ms
64 bytes from 192.168.6.130: icmp_seq=3 ttl=64 time=1.81 ms
64 bytes from 192.168.6.130: icmp_seq=4 ttl=64 time=0.077 ms
64 bytes from 192.168.6.130: icmp_seq=5 ttl=64 time=0.100 ms
64 bytes from 192.168.6.130: icmp_seq=6 ttl=64 time=0.175 ms
64 bytes from 192.168.6.130: icmp_seq=7 ttl=64 time=0.101 ms
64 bytes from 192.168.6.130: icmp_seq=8 ttl=64 time=0.272 ms
64 bytes from 192.168.6.130: icmp_seq=9 ttl=64 time=0.178 ms
64 bytes from 192.168.6.130: icmp_seq=10 ttl=64 time=0.104 ms

--- 192.168.6.130 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9216ms
rtt min/avg/max/mdev = 0.077/0.359/1.809/0.508 ms
# !/bin/bash

echo script has begin

ping -c 10 192.168.6.130

cat testscript.sh

echo script has been ended......
script has been ended......

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to create a Shell and Subshell on Rocky Linux 9.2. Your feedback is much welcome.

FAQ
Q
Why shell scripting?
A
Using a shell script is most useful for repetitive tasks that may be time-consuming to execute by typing one line at a time.
Q
Is Python faster than bash?
A
Bash shell programming is the default terminal in most Linux distributions; thus, it will always perform faster.
Q
Which language is bash?
A
Bash is not a programming language, it is a command-line interpreter. Bash is a software replacement for the original Bourne shell. Python is an easy, simple, and powerful language. Bash is tough to write and not as powerful as Python.
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.
Q
What is Bash Scripting?
A
A Bash Shell Script is a plain text file containing a set of various commands that we usually type in the command line. It is used to automate repetitive tasks on the Linux filesystem. It might include a set of commands or a single command, or it might contain the hallmarks of imperative programming like loops, functions, conditional constructs, etc. Effectively, a Bash script is a computer program written in the Bash programming language.