Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Linux Shell?
3.
Shell Prompt
4.
Shell Scripting
5.
Shell Substitution
5.1.
Escape Sequences
5.2.
Command Substitution
5.3.
Variable Substitution
6.
Frequently Asked Questions
6.1.
Why is “-e” used with the echo command?
6.2.
How to write comments in the scripts?
6.3.
What is the proper way to substitute a command within a command?
6.4.
How do I print messages as a standard error in Linux?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Linux Shell Substitution

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

This article aims to increase the reader's understanding of the Linux shell, the scripting done on the shell, and the substitution methods used in the shell scripting. When we have a sequence of commands to execute, we use scripting to automate the process of executing commands.

Introduction

So, first of all, we will start with what Linux shell is, then learn what scripting is and how it is done, and then discuss the substitutions we use in scripting.

What is Linux Shell?

Linux shell is the command interpreter present in Linux/UNIX-like operating systems. We use the shell as an environment to execute commands, programs, and scripts. The shell takes input from the user and executes the commands and programs based on that input. And when this execution finishes, the shell displays the output for the same.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Shell Prompt

Prompt

The shell prompt, $, is also known as the command prompt. The Linux shell issues it, and the user can type commands while the command prompt is displayed. Through the prompt, the shell reads the user inputs, and when the user presses Enter, the shell executes it and gives the output to the user. The user can customize the prompt according to their taste using the PS1 environment variable. The PS1 variable is employed to alter the shell command prompt's appearance and setting.

Shell Scripting

Scripting

The main concept of shell scripting is listing the commands we want to execute. These commands are to be listed in the order of their execution. A good shell script is a script that contains comments so that some other programmer can easily understand the code. The syntax for comments in shell scripts starts with the symbol. 

In a script, we can implement conditional statements, loops, file input-output, and many other functionalities similar to that in C/C++. The shell script consists of three components:

  • Control Flow: This includes if, else, then, shell loops, etc.
     
  • Shell Commands: These include commands like ls, cd, pwd, echo, etc.
     
  • Shell Keywords: These consists of keywords like if, else, break, etc.

Shell Substitution

Shell substitution is the process in which the shell substitutes an expression that consists of one or more special characters. We use this functionality to instruct the shell to replace the actual value of an expression. 

Example:

#!/bin/sh
x=1
echo -e "x = $x\n"

Output:

Command Output-exa

Let us see some variations of the above code.

Code:

#!/bin/sh
x=1
echo -e "x = $x\n"
echo "x = $x\n"
echo -E "x = $x\n"
echo -n "x = $x\n"

Output:

Command Output-example

In the above example, “-e” enables the interpretation of the backslash escapes.

-E” disables the interpretation of the backslash escapes (default).

-n” disables the insertion of the new line.

\n” is a type of escape sequence.

Escape Sequences

Escape sequences are sequences of characters that do not represent their true nature but have some special meaning to the OS. Some of the most widely used escape sequences with echo commands are:

  • \\”: Backslash
     
  • \a”: Alert (BEL)
     
  • \b”: backspace
     
  • \c”: suppress trailing newline
     
  • \f”: form feed
     
  • \n”: new line
     
  • \r”: carriage return
     
  • \t”: horizontal tab
     
  • \v”: vertical tab
     

There are two primary techniques used in shell scripting, namely:

  • Command Substitution
     
  • Variable Substitution
     

Command Substitution

The mechanism by which the shell executes a given set of commands and then substitutes their outputs in the place of the commands is called command substitution. Let us understand with an example: When a command is enclosed with a pair of backticks, the shell executes the command first, and the output of the command replaces the enclosed command text. It can be considered as running a command within another command. 

Syntax:

`command` 
 

Example: 

#!/bin/sh 
echo “Today’s date is `date`.”    

Output:

Command Output-substitution command

In this example, the shell first executes the enclosed command in backticks,i.e., date, and then puts the output in its place. 

While using command substitution, remember that backticks are used here and not single quote characters.

Variable Substitution

This substitution process allows the programmers using shell, to manipulate the value of variables based on their states. Since throughout execution, the value assigned to a particular variable might get modified by changing the variable's state, variable substitution helps us handle those variables' changes.

The possible substitutions are:

  • ${var}: Substitutes the value for var
     
  • ${var:-word}: If var is null or unset, the word is substituted for var. The value of var does not change.
     
  • ${var:=word}: If var is null or unset, var is set to the value of the word.
     
  • ${var:+word}: If var is set, the word is substituted for var. The value of var does not change.
     
  • ${var:?message}: If var is null or unset, the word is printed to standard error. This checks that variables are set correctly.
     

Example:

#!/bin/sh

echo ${var:-"Coding Ninjas"}
echo "1 - Var is ${var}"

echo ${var:="Coding Ninjas"}
echo "2 - Var is ${var}"

var = 1
echo ${var:+"Coding Ninjas"}
echo "3 - Var is $var"

unset var
echo ${var:?"message"}
echo "4 - Var is ${var}"

Output:

Check out this problem - Shortest Common Supersequence.

Frequently Asked Questions

Why is “-e” used with the echo command?

When we have backslash escapes in our command, we use -e because it acts as an interpreter for them. Without -e shell would simply print \n and not interpret its meaning of a new line.

How to write comments in the scripts?

To write comments in the scripts along with the commands, we follow syntax and start our comment line with “#” so that the shell can skip those lines and execute the rest of the lines of commands.

What is the proper way to substitute a command within a command?

To do command substitution, meaning to use a command within a command, we need to enclose the command with backticks (``). This should not be confused with single inverted commas.

How do I print messages as a standard error in Linux?

In the Linux shell, we use ${var:?message} to print a message as a standard error message. This will give the message as a standard error if the var is unset(or null). 

Conclusion

In this article, we discussed the Linux shell, its prompt, and how scripting is done on it, and also we studied the Linux Shell Substitution deeply. We discussed their types, usefulness, and how to use them.

See more,  ping command in linux

We hope this blog is helpful to you and increase your knowledge about shell substitution and its commands. Do upvote our blogs if you find them helpful and engaging! For more about Linux, go to Linux OSTypes of UNIX OS, and Intro to Linux Shell.

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Keep learning, and Keep Growing!

Happy Learning!

Previous article
Linux - Shell Loop Control
Next article
Grep Command in Linux/UNIX
Live masterclass