BASH Programming

Bash Subshells

Sometimes, it requires you to run one or more commands or scripts in the background. This task can be done by writing all commands or scripts into a script file and executing the file using ampersand (&) or executing the commands or scripts into the subshell from the current shell. The subshell can be used to execute multiple commands or scripts by enclosing with the $() or the backticks (`). The methods of using the Bash subshell are shown in this tutorial.

Different Examples of Subshell

The different ways of executing the script in the subshell are shown in this part of the tutorial.

Example 1: Execute the Subshell Using the Single Quote and Double Quote

Create a Bash file with the following script that prints the current date and time. Next, the $strVal variable is printed by enclosing the subshell script within the single quotes and the double quotes.

#!/bin/bash

#Print the `date` command in the subshell

echo "Today is `date`"

#Define a string variable

strVal="Bash Subshell"

#Print the variable in the subshell using single quotes

echo "The output of single quotes:" '$(echo $strVal)'

#Print the variable in the subshell using double quotes

echo "The output of double quotes:" "$(echo $strVal)"

The following output appears after executing the script. The subshell script is printed as a string when it is enclosed with the single quotes. The subshell script is executed when it is enclosed with the double quotes:

Example 2: Search All Files of the Particular Extension Using the Subshell

Create a Bash file with the following script that takes the file extension as an input from the user. Next, the “ls” command is executed in the subshell to search all files of that particular extension.

#!/bin/bash

echo -n "Enter the name of the file extension:"

#Take the file extension name that will be searched

read ext

#Check the input value

if [[ $ext == "" ]]; then

#Print error message

echo "No extenion is given."

else

echo "Filenames with $ext extension:"

#Print all filenames with the given extension

echo "$( echo `ls *.$ext` )"

fi

The following output appears after executing the script with the “txt” input. According to the output, three text files exist in the current location:

The following output appears after executing the script with the empty value:

Example 3: Execute the Arithmetic Expression in the Subshell

Create a Bash file with the following script where the variable of the same name is used in the main shell and the subshell. The arithmetic operation is defined in the main shell and the subshell.

#!/bin/bash

#Define a parent shell variable

number=10

#Print the result based on the variable of the parent shell

((result=$number+5))

echo "The sum of $number+5=$result"

#Create a subshell variable with the same name of the parent shell

( number=20 ; ((result=$number+10)); echo "The sum of $number+5=$result" )

#Print the result based on the variable of the parent shell again

echo "The sum of $number+5=$result"

The following output appears after executing the script. The first and the last outputs show the result of the main shell. The second output shows the result of the subshell. The variable of the main shell is not modified by the variable of the subshell:

Example 4: Execute Multiple Commands in the Subshell

Create a Bash file with the following script that sends the output of the “echo” command into the “sed” command that replaces the matching string value with another string. The output of the “echo” command is “JavaScript”. So, this value is compared with “Java” and “JavaScript”. If a match is found, the matching strings are replaced by the “Type” string.

#!/bin/bash

#Define a string value

strVal="JavaScript"

#Print the original string value

echo "String value: $strVal"

#Print the subshell value

echo -n "Subshell value: "

echo "$(echo $strVal | sed 's|Java|JavaScript Type|')"

The following output appears after executing the script. According to the output, the “Java” string is replaced by the “Type” string. The output of the subshell is “JavaScript TypeScript”:

 


Conclusion

One or more commands or scripts can be executed using the subshell without affecting the main shell. The purposes of using the subshell are explained in this tutorial using multiple examples. Different types of tasks such as searching the files, calculating the sum of numbers, replacing the strings, etc. are done by the subshell in the given examples. The concept of using the subshell is properly demonstrated and the new Bash users will now be able to use the subshell after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.