Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In this blog, we will discuss the quoting mechanism in Linux. Linux is based on shell scripting, and if you are new to the Linux environment and want to switch to Linux, then you need to know about shell scripting. Let's talk about Linux in brief.
What is Linux?
Linux is an open-source kernel that Linus Torvalds developed. A kernel is a software whose role is to communicate between the application and hardware of a system. By open source, I mean its source code is publicly available, which you can contribute to if you have enough knowledge about Linux
Linux is a highly customizable kernel with various distributions, like Debian, Ubuntu, Kali, and many more. In today's world, Linux is used on multiple devices around you; for example, android devices are based on Linux
Quoting Mechanism is one of the essential aspects to know about if you are working with the shell. To learn about shell scripting, you can go toIntroduction to shell scripting.
Quoting Mechanism
First, understand what we mean by quoting mechanism in the shell. When you work in the Linux environment, there are some special characters or metacharacters that you might know. Quoting mechanism removes the specialty or functionality of these special characters so that we can use them usually rather than special characters in the shell.
These special characters are already defined in the Linux terminal, which means they got assigned to some special operation they will perform if we type them in the terminal.
For example,
Semicolon ( ; ) in the terminal separates two commands.
As you can see in the above image, we are using the ls and cd commands simultaneously with a semicolon; it is working.
When we try to print the welcome;Ninja text in the terminal using the echo command, there is an error message Ninja: not found. This is because we are using ; which is a metacharacter, and the shell is processing it as a special character.
Metacharacters
Metacharacters
Description
|
| characters pipe the two commands as one.
;
Semicolon separates the two different commands in one statement.
>
Shows the output direction in the terminal.
<
Shows the input direction in the terminal.
&
puts the current command in the background of the system.
||
Use the OR conditional statement.
&&
Use the AND conditional statement.
?
Matches and replaces the for one character.
Need for Quoting Mechanism:
When coding your shell script, you might want to use these special characters but not with their functionality, as discussed in the semicolon example. There can be cases where you add a semicolon ; or ? or any other special character, but you do not require their functionality, then you will need the quoting mechanism.
Example
First, we create a shell script using the vi editor named sample.sh.
Now we will try to echo my name is; dhananjay, on the console.
As you can see in the image below, it is not displaying on the console when we execute the sample.sh file instead shows dhananjay not found because the semicolon has a specific functionality pre-defined in the shell scripting.
How can we use the metacharacters or special characters without executing their predefined functionality?
We have a few solutions available to us that we can use and execute even the special characters and avoid their specialty.
Types of Quoting Mechanisms
There are multiple solutions available to execute the quoting mechanism in the shell. Let's discuss each of them and why we need more than one solution to use quoting mechanism.
Backslash \
With the help of backslash \, we can avoid a special character in the sentence or command. Let's see how we can achieve it.
In the below image, we are trying to echo hello & welcome to coding ninjas and see we can only display the hello because & is used to put the processes in the system's background.
Let's try this again, but this time we will echo hello \& welcome to coding ninja. As you can see below, we successfully executed the echo command this time.
But backslash is not ideal in every case because what if there are multiple special characters in the sentence?
For example, what if you need to echo the sentence below with these many special characters?
Well, it is possible to execute the above echo command with backslash \, but it can make your command unreadable and complicated, like in the image below.
A backslash \ is good when there are one or three special characters at some distance from each other. Let's try one more solution.
Single Quotes ‘ ‘
Our problem of multiple special characters in a command can be solved with single quotes. You must put the sentence between the single quotes, and your work is done.
As you can how easy, it is to use single quotes to avoid multiple special characters.
But there is a drawback of using the single quotes mechanism, which is that if there are multiple single quotes in a sentence, then the console will be confused, and we will not be able to execute the command.
Double Quotes “ “
We can use the double-quest mechanism to avoid the single-quote problem and ignore the special characters.
But double quotes also face the same problem as a single quote; that is, if there are multiple double quotes, then the quoting mechanism will not work anymore.
As you can see in the image below echo command has multiple double quotes, and we cannot display anything on the console.
Also, there will be some characters that will remain special even after using the double quotes. The special characters $, \$, \, \', \", ! and the backtick “`” will remain the same even after using the double quotes.
Backquotes ` `
Backquotes in shell scripts are used to execute a command mentioned in the backquotes. When we type something between the backquotes, it is treated as a command, and the terminal will execute it.
We will use the date command as an example. When we type the date in the terminal, it will show us the current date and time.
When putting the date command in the backquotes, you can see it still got executed and displays the date and time.
When you put the backquote command in the double quotes, it still got executed because double quotes do support the backquotes as special characters, but single quotes do not support the backquotes, and it will not get executed as a date command.
Keep in mind that these characters we have discussed to execute the quoting mechanism are also part of the special characters, so can use them to nullify each other functionality.
Frequently Asked Questions
Can we backslash with a single quote to remove its specialty?
Yes, we can use a backslash to remove the specialty of a single quote.
Do double quotes support all the special characters?
The special characters $, \$, \, \', \", ! and the backtick “`” will remain the same even after using the double quotes.
Can we use multiple backslashes in a command?
You can use multiple backlashes in a command if required, but it will make your command look complex.
Are these metacharacters predefined?
Yes, these metacharacters are predefined in the shell scripting.
Conclusion
In this blog, we discussed what Linux is and shell scripting in Linux. We have also discussed the quoting mechanism in Linux and also how to implement the quoting mechanism. We have also talked about the metacharacter with their descriptions.
To learn more about Linux commands, check out the following articles.