Shell Scripting - Variable Substitution
Last Updated :
27 Jan, 2022
A shell is an interface that helps users to connect with the system. Using a shell is equivalent to indirectly communicating with the operating system. In Linux distributed systems, each time we use a terminal, we connect with a shell. The job of a shell is to analyze Unix commands or instructions given by the user. This process involves taking commands from the user and converting them into a form that the kernel can easily understand. In simple words, it acts as a medium between the user and the kernel of the operating system. The kernel is the most crucial part of a computer’s operating system.
In order to understand variable substitution, let us first discuss substitution in shell scripts. Substitution is a functionality by following which we can instruct the shell to substitute the actual value of an expression.
Substitution of escape sequences:
There are some sequences of characters that don't represent their true nature but they have special meaning to the operating system and these sequences are known as escape sequences. When they are used in a command, they are replaced by actual values.
Escape Sequences | Significance |
---|
\n | add a new line |
\t | horizontal tab |
\\ | backslash |
\b | backspace |
\r | carriage return |
Example:
In this script, we used two echo commands to print strings. As you can see we have used an escape sequence for the new line (\n) at the end of the string. Since the actual job of this character is to add a new line hence new line is added (clearly visible in the output). Also, in the second statement, we have used three horizontal tab characters in the beginning. Since the actual job of this character is to add a horizontal tab space hence three horizontal spaces are added before printing the string to console (clearly visible in the output).
#!/bin/sh
#statement 1
echo -e "GeeksforGeeks\n"
# statement 2
echo -e "\t\tis the best"
Output:

Variable substitution:
A shell allows us to manipulate the value of a variable based upon its initialization status. By initialization status we mean, whether a variable is initialized before its value is actually used for different purposes.
Sr No. | Expression | Significance |
---|
1 | ${myVariable} | substitute the value of myVariable. |
2 | ${myVariable:-value} | If myVariable is not-set (or null) then the value is substituted for myVariable. |
3 | ${myVariable:=value} | If myVariable is not-set (or null), then it is set to value. |
4 | ${myVariable:+value} | If myVariable is set then the value is substituted for myVariable. |
5 | ${myVariable:? message} | If myVariable is not-set (or null) then the message is printed as standard error. |
The working of these expressions has been discussed in detail in the below script:
Example:
#!/bin/sh
# If myVariable is unset or null then
# assign 100 to it
echo ${myVariable:- 100}
echo "1. The value of myVariable is ${myVariable}"
# If myVariable is unset or null then
# assign "GeeksforGeeks" to it
echo ${myVariable:="GeeksforGeeks"}
echo "2. Value of myVariable is ${myVariable}"
# unset myVariable
unset myVariable
# If myVariable is set then substitute
# the value
echo ${myVariable:+"GeeksforGeeks"}
echo "3. Value of myVariable is $myVariable"
myVariable="GeeksforGeeks"
# If myVariable is set then substitute the value
echo ${myVariable:+"Nainwal"}
echo "4. Value of myVariable is $myVariable"
# If myVaraible is not-set or null then print
# the message
echo ${myVariable:?"Message"}
echo "5. Value of myVariable is ${myVariable}"
# unset myVariable
unset myVariable
# If myVaraible is not-set or null then print
# the message
echo ${myVariable:?"Message"}
echo "6. Value of myVariable is ${myVariable}"
Output:
Similar Reads
Shell Scripting - Substitution There are certain expressions that convey special meanings. In other words, they are not what they look like. A shell carries out substitution whenever it encounters such expressions. Hence, substitution is defined as a mechanism carried out by a shell in which it substitutes the value of an express
3 min read
Shell Scripting - Command Substitution A shell is an interface that helps users to connect with the system. Using a shell is equivalent to indirectly communicating with the operating system. In Linux distributed systems, each time we use the terminal, we connect with a shell. The job of a shell is to analyze Unix commands or instructions
4 min read
Shell Scripting - Shell Variables A shell variable is a character string in a shell that stores some value. It could be an integer, filename, string, or some shell command itself. Basically, it is a pointer to the actual data stored in memory. We have a few rules that have to be followed while writing variables in the script (which
6 min read
Shell Scripting - Local Variables The shell is a command-line interpreter for Linux and Unix systems. It provides an interface between the user and the kernel and executes commands. A sequence of commands can be written in a file for execution in the shell. It is called shell scripting. It helps to automate tasks in Linux. There are
3 min read
Shell Scripting - Subshell 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. A subshell is a child shell that is spawned b
4 min read
Shell Scripting - Default Shell Variable Value A shell gives us an interface to the Unix system. While using an operating system, we indirectly interact with the shell. On Linux distribution systems, each time we use a terminal, we interact with the shell. The job of the shell is to interpret or analyze the Unix commands given by users. A shell
3 min read