The Wayback Machine - https://web.archive.org/web/20240607094439/https://www.geeksforgeeks.org/shell-scripting-variable-substitution/
Open In App

Shell Scripting – Variable Substitution

Last Updated : 27 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 - Difference between Korn Shell and Bash shell
Korn Shell: Korn Shell or KSH was developed by a person named David Korn, which attempts to integrate the features of other shells like C shell, Bourne Shell, etc. Korn Shell allows developers to generate and create new shell commands whenever it is required. Korn shell was developed a long year back as it is older than the BASH shell and it has fe
3 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 accepts commands from the user and transforms them
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 given by the user. This process involves taking c
4 min read
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 expression with its actual value. Escape sequences: An es
3 min read
Shell Scripting - Restricted Shell
Shell is one of the most important and powerful tools available in GNU/Linux-based systems. One can control the entire system if used correctly, this exact feature makes the shell a double-edged sword. It can potentially harm the system if one executes a system command without knowing its underlying details and consequences. To prevent this we use
5 min read
Shell Scripting - Interactive and Non-Interactive Shell
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 accepts commands from the user and transforms them
3 min read
Shell Scripting - Shell Signals Values
Prerequisites: Processes, Bash Scripting, Shell Function Library Signals help the operating system to communicate with processes and vice-versa. Signals are also generated when processes don't function properly or try to access prohibited memory. Different signals are mapped to different numbers which are referred to as signal values. The Linux OS
6 min read
Introduction to Linux Shell and Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understanding shell scripting we have to get familiar with th
7 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 will be discussed in the article). Overall knowing
6 min read
Shell Scripting - Rules for Naming Variable Name
Variables are quite important in any script or program, so we need to understand what is the convention to name these variables. There are certain rules and standards to keep in mind while giving names to the variables in Shell scripting. In this article, we will discuss and list down all the rules and conventions to declare a variable name in Shel
4 min read
Article Tags :