Bash Scripting: Variables and Subshell
It is quite easy to declare a variable in Bash shell, take an example like one below:
#! /bin/bash
myname="Kshitiz"
myage="26"
echo "My name is: $myname"
echo "my age is: $myage"
We can also store the output of a valid shell command to a variable, this process is called Subshell.
Here, a valid shell command is sent to the background and its output is then pointed by a variable. Let’s understand that by an example:
#! /bin/bash
currentpath=$(pwd)
currentdate=$(date)
echo $currrentpath
echo $currentdate
The output of the above commands is shown below:
We know that a shell runs inside an environment and that environment by default has a bunch or variables pre-defined. To access those variables, we do the ffollowing:
#! /bin/bash
echo "Listing out all the environment variables below:"
env
echo $COMPUTERNAME
This wraps up the utilization of variables and how variables can be used to point to a subshell output.