Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
Shell 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
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.
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:
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}"
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.
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 OS, Types of UNIX OS, and Intro to Linux Shell.