Open In App

Shell Scripting – Substitution

Improve
Improve
Like Article
Like
Save
Share
Report

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 escape sequence is a group of character(s) that does not represent its actual value when it is used as a string literal. Some of the escape sequences are listed below:

Sr. No. Escape Sequences Significance (actual value)
1 \n new line
2 \f form feed
3 \r carriage return
4 \b backspace
5 \t horizontal tab
6 \v vertical tab
7 \\ backslash

Shell substitutes an escape sequence with its actual value.

Example:

In this shell script, firstly we have used an echo command to print a string. Note that we have used an escape sequence ( \n ) at the end of the string. It will add a new line character after printing the string.

#!/bin/sh

// Print the string
echo -e "Hello World! \n"
  
// Print the string
echo -e "Hello GeeksforGeeks"

Output:

Variable Substitution:

The shell allows us to manipulate the value of a variable based upon its initialization status. 

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:? message} If myVariable is not-set (or null) then the message is printed as standard error.
5 ${myVariable:+value} If myVariable is set then the value is substituted for myVariable.

Example:

These expressions are demonstrated in the below shell script.

#!/bin/sh


# If myVariable is unset or null 
# then assign 12 to it
echo ${myVariable:- 11}
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:+"Bhuwanesh"}
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

# If myVaraible is not-set or null then
# print the message
echo ${myVariable:?"message"}
echo "6. Value of myVariable is ${myVariable}"

Output:

Command Substitution:

Command substitution is a mechanism that is followed by programmers in a bash script. In this mechanism, the output of a command replaces the command itself. Bash operates the expansion by executing a command and then replacing the command substitution with the standard output of the command. In simple words, the output of a UNIX command is bundled and then used as a command.

To understand it in a better way, let us consider an example. The seq command in Linux is used to print numbers from START to END in steps of INCREMENT.

Syntax:

seq START INCREMENT END  

Return type:

Prints numbers from START to END each in the new line by the difference of INCREMENT.

Example:

In the below script we are printing numbers from 2 to 20 with a difference of 2. In other words, we are printing even numbers up to 30.

#!/bin/bash

# your code goes here

seq 2 2 30

Output:

We can use the output of the above command as a new command. Consider the below script,

Example:

#!/bin/bash
# your code goes here
 
echo $(seq 2 2 20)

Output:


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