# UNIX / Linux Shell Substitution - Unix/Linux

## What are UNIX / Linux Shell Substitution?

### What is Substitution?

The shell performs substitution when it encounters an expression that contains one or more particular font.

### Example

Here, the printing value of the variable is substituted by its value. Same time, "\n" is substituted by a new line –

You will collect the following result. Here the -e selection enables the interpretation of backslash escapes.

Following is the result without -e option –

Here are following escape sequences which can be used in echo command −

S.No. Escape & Description
1
\\
backslash
2
\a
3
\b
backspace
4
\c
suppress trailing newline
5
\f
form feed
6
\n
new line
7
\r
carriage return
8
\t
horizontal tab
9
\v
vertical tab

You can use the -E selection to disable the interpretation of the backslash escapes (default).

You can use the -n selection to disable the introduction of a new line.

### Command Substitution

Command substitution is the instrument by which the shell performs a given set of commands and then substitutes their output in the place of the commands.

### Syntax

The command substitution is perform when a command is given as –

When performing the command substitution make sure that you use the back quote, not the single quote character.

### Example

Command substitution is normally used to consign the output of a command to a variable. Each of the following examples demonstrates the command substitution –

Upon execution, you will collect the following result –

### Variable Substitution

Variable substitution enables the shell programmer to control the value of a variable based on its state.

Here is the following table for all the feasible substitutions −

S.No. Form & Description
1
${var} Substitute the value ofvar. 2${var:-word}
Ifvaris null or unset,wordis substituted forvar. The value ofvardoes not change.
3
${var:=word} Ifvaris null or unset,varis set to the value ofword. 4${var:?message}
Ifvaris null or unset,messageis printed to standard error. This checks that variables are set correctly.
5
\${var:+word}
Ifvaris set,wordis substituted for var. The value ofvardoes not change.

### Example

Following is the instance to show different state of the above substitution –

Upon implementation, you will accept the following result –