Command Substitution

Command substitution allows us to take the output of a command or program and save it as the value of a variable. Below is the example for the same –

Example Script:

var=$( ls /usr | wc -l )
echo Total $var entries on usr directory


Output of Command Substitution:

Total 13 entries on usr directory


In the above example, we have stored the result of ls /usr | wc -l , in a variable.

Below is the terminal shell depiction after executing the script –

 

Bash Script – Working of Bash Variables

A Bash script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a reusability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can use that file for executing the set of commands one or more times as per our requirements. In programming, a variable is a value that can change, depending on conditions or on information passed to the program. A variable in bash can contain a number, a character, or a string of characters. Here in this article, we are going to discuss the working of variables within Bash Scripting.

Rules for defining variables in Bash Scripts are as follows –

  • Variable names can contain uppercase, lowercase letters, numbers, underscores, and digits.
  • It is a good practice to use uppercase letters for variable names within Bash scripts.
  • Space is not allowed.
  • Pre-defined keywords cannot be used. Like if, else, etc.

The simplest example of the use of variables in Bash scripting can be given as –

Example Script:

NAME="Satyajit Ghosh"
echo "His name is $NAME"


Output:

His name is Satyajit Ghosh


The above example shows a variable ‘name‘. Then it is used with an echo command to display its value. So, the basic syntax for writing variables within a Bash Script will be –

Syntax of Writing Variables:

VariableName=value
echo $VariableName #for accessing the value


If we want to change the value of a variable then we can do that. The basic syntax for that will be –

VariableName=value
VariableName=newValue
echo $VariableName #for accessing the value


Below is an example of the same –

Example Script:

AGE=10 #assigning a value
AGE=20 #changing the value
echo $AGE #display the value


Output:

20


Below is the terminal shell depiction after executing the script –

 

Similar Reads

Variable Scope

The scope in a program or script is a region where the variables have their existence. If a variable is declared inside a function then it is generally a local variable and if it is declared outside then it is a global variable. In the case of a bash script, this concept is a little bit different, here any variable whether it is written inside a function or outside a function by default is a global variable. If we want to make a local variable then we need to use the keyword “local”....

Operations on Variables

We can perform both numerical and string operations on variables on Bash scripting. An example of the same is given below –...

Special Variables

There are some special types of variables present in the brush. These are also called internal variables or preset variables. Below is the list of some of them....

Command-line Arguments

We can supply arguments during the execution of the script and those variables can control the behavior of the script. Suppose we have a script as follows –...

Command Substitution

Command substitution allows us to take the output of a command or program and save it as the value of a variable. Below is the example for the same –...

Exporting Variables

Variables are limited to the process they were created in. If we want the variable to be available to another script then we need to export the variable. Below is an example of the same –...