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:


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.

Similar Reads

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:...

Variable Substitution:

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

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....

Contact Us