What is Shell and Sub Shell in Bash Script

What Is Shell And Sub Shell In Bash Script

Shell: Shell scripting is a powerful tool for automating tasks and simplifying the management of systems and applications. One important concept in shell scripting is the use of subshells, which allow you to execute commands within a separate shell environment. Subshell: A 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 it allows you to execute commands and perform operations within a separate environment.

Procedure Steps:

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

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 12 (bookworm)
Release:	12
Codename:	bookworm

Step 2: Create a file for create a script by using following command.

root@linuxhelp:~# vim subshell
#!/bin/bash
# Show the Current Directory in the parent shell
pwd

# Create a subshell and Change to a different directory
( cd /home/linuxhelp ; pwd )

# The Current directory in the parent shell is unchanged.
pwd

Step 3: Give the Executable permission to the script file by using following command.

root@linuxhelp:~# chmod +x subshell 

Step 4: Long list files to check file permission by using following command.

root@linuxhelp:~# ls -la
total 40
drwx------  4 root root 4096 Sep 25 05:43 .
drwxr-xr-x 19 root root 4096 Sep 16 00:52 ..
-rw-------  1 root root  653 Sep 25 05:36 .bash_history
-rw-r--r--  1 root root  571 Apr 11  2021 .bashrc
drwx------  2 root root 4096 Sep 12 00:35 .cache
-rw-r--r--  1 root root  161 Jul  9  2019 .profile
drwx------  2 root root 4096 Sep 12 00:17 .ssh
-rwxr-xr-x  1 root root   52 Sep 25 05:43 subshell
-rw-------  1 root root 7255 Sep 25 05:43 .viminfo

Step 5: Run the script file by using following command.

root@linuxhelp:~# ./subshell 
/root
/home/linuxhelp
/root

Conclusion:

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

FAQ
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 a shell in an operating system?
A
The shell is the outermost layer of the operating system. Shells incorporate a programming language to control processes and files and start and control other programs
Q
What are the different types of Shells?
A
C shell (csh)
TC shell (tcsh)
Korn shell (ksh)
Bourne Again SHell (bash)
Q
What is the use of Shell Scripting?
A
It's called a shell script because it combines into a "script" in a single file a sequence of commands that would otherwise have to be presented to the system from a keyboard one at a time. The shell is the operating system's command interpreter and the set of commands you use to communicate with the system.
Q
What is Shell Scripting?
A
A shell is a special program that acts as an interpreter between the user and the operating system. Shell accepts human-readable commands from a user and converts them into Kernel-readable language. It is a command language interpreter that executes commands read from input devices such as keyboards or from files.