How to Do Bash Variable Substitution (Parameter Substitution)
Last updated
Last updated
When the original value of any expression is substituted by another value then it is called substitution. Bash variable substitution or parameter substitution is a very useful feature of bash. Generally, it is done in the bash by using the '$' character and the optional curly brackets ({}). The content of the bash variable can be checked or modified based on the programming requirements by using variable or parameter substitution.
In this tutorial, we will learn different ways of substituting bash variables.
A variable is a container that is used to store a particular value temporarily. No data type declaration is mandatory to declare bash variables. A variable name may contain characters, numbers, and underscores ( _ ). The ‘$’ symbol is not used to declare or assign a bash variable but the ‘$’ symbol is used to read the content of the assigned variable.
Any string or number can be assigned in a bash variable by using the assignment operator without any space. The ‘name’ variable assignment is valid because no space was not used before and after the ‘=’ symbol. The ‘quantity’ variable assignment is invalid because the space was used before and after the '=' symbol.
#Valid variable assignmentname="Exampledotcom"#Invalid variable assignmentQuantity = 100
The value of the ‘name’ variable (previously assigned) has been printed here by using the `echo` and `printf` commands.
#Print the variable using `echo`echo
$name#Print the variable using `printf`printf
"The name of the website is %s\n"
$name
At first, the value of the $name variable which is 'Exampledotcom', has been printed without concatenating by any string value. Next, the $name variable has been printed by concatenating it with a string value.
The way of substituting the value of a variable by using another variable or command or parameter expansion is called bash variable substitution. We have discussed different types of bash variable substitutions in this section of this tutorial.
Two different ways of substituting variables with string values have been shown here.
Substitute variable by '$' symbol
You can substitute the bash variable with another variable by reading the value of the variable by using the '$' symbol. The values of two variables have been substituted with another string value into a variable in the following script.
#Assign a string valuestring1="Bash"
#Assign another string valuestring2=" Variable"#Substitute the variablessub="$string1$string2 Substitution"#Print the substituted valueecho
"The substituted value is '$sub'"
The value of the $string1 variable is 'Bash' and the value of the $string2 variable that is ' variable' have been substituted with the string value, 'Substitution' into the $sub variable.
Substitute variable using the parameter
You can substitute bash variables by using “${parameter}”. In the following script, three variables have been initialized with three string values and substituted into another variable that has been printed later.
#Assign the first variablevariable1='Bash'#Assign the second variablevariable2=' Programming'#Assign the third variablevariable3=' Script'#Substitute the variablessub="${variable1}${variable2}${variable3}"#Print the substituted valueecho
"The substituted value is '$sub'"
The value of $variable1 is 'Linux', the value of $variable2 is 'Op', and the value of $variable3 is 'Sys'. The values of these variables have been substituted into the variable, $sub.
The command substitution is another way of substituting variables in bash. The ‘$()’ is used for applying command substitution. In this case, the variable is substituted by the output of a command. There are some differences between parameter substitution (‘${}’) and command substitution (‘$()’) which have been discussed in this section.
Difference between ‘${parameter}’ and ‘$(command)’:
It is defined by ‘${}’.
It is defined by ‘$()’.
It is used to store the value by associating it with a name, number, or special character.
It is used to store the output of any command.
It is useful when we require to add one or more characters or numbers immediately after the parameter value. For example: str="Book"echo "${str}s" Output: Books
It is useful when we require to add one or more characters with the output of any command. For example: str=$(pwd)echo "Current working directory: $str" Output: Current working directory /home/username
Use of command substitution:
The username of the currently logged-in user will be substituted in the $user variable by executing the command, `whoami` and the value of this variable will be printed later.
#Read the logged-in usernameuser=$(whoami)#Print the current usernameecho
"The current username is $user"
According to the output, the currently logged-in username is 'ubuntu'.
Many ways exist in bash to substitute the variable by defining the default shell variable. Three of them are ":=", ":-", and ":+". The ":=" is used to print the newly assigned value if the variable is not set before. The ":-" is used to print the previously assigned value if the variable is set before. The ":+" is used to print the newly assigned value if the variable is set before.
#Newly assigned value will be printed if the variable is unsetecho
"${OS:=Ubuntu}"#Previously assigned value will be printed if the variable is setecho
"${OS:-CentOS}"#Newly assigned value will be printed if the variable is setecho
"${OS:+Debian}"
Here, the $OS variable was not initialized before. So, 'Ubuntu' has been printed in the first output. 'Ubuntu' has been printed in the second output because the $OS was set. 'Debian' has been printed in the third output because the $OS was set.
An array of numbers has been defined and the value of the third index of the array has been printed by array variable substitution in the following script.
#Declare an array of 6 numbersarr_variable=(45 90 34 12 90 51)#Print the 3rd element value of the arrayecho
"The value of 3rd element is: ${arr_variable[2]}"
The 3rd index value of the array is 34 which has been printed here.
The way of substituting a variable with another variable with the string enclosed by a single quote ( ' ) has been shown in the following script.
#Assign a variablestring="Substitution"#Substitute the variable with single quotessub='Variable '$string' tutorial'#Print the substituted valueecho
"The substituted value is '$sub'"
Here, the value of the $string variable has been substituted by adding it in the middle of two strings, ‘Variable ‘ and ‘ tutorial’.
The variable can be substituted in different ways by using parameter expansion options. The use of parameter expansion to substitute the part of a variable has been shown in the following script.
#Assign a string value to a variablestring="Bash is a scripting language"#Find and replace the particular part of a variablesub="${string/scripting/popular}"#Print the substituted valueecho
"The substituted value is '$sub'"
The value of $string is 'Bash is a scripting language' that has been substituted into the $sub after replacing the word, 'scripting' with 'popular' by using parameter expansion.
Bash variable substitution has been explained in multiple ways in this tutorial to clear the concept of variable substitution and it will help you to substitute variables in your script on demand.