Open In App

Shell Scripting – Variable Substitution

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:


Last Updated : 27 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads