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?
3.
Quoting Mechanism
3.1.
Metacharacters
3.2.
Need for Quoting Mechanism:
4.
Types of Quoting Mechanisms
4.1.
Backslash \
4.2.
Single Quotes ‘ ‘
4.3.
Double Quotes “ “
4.4.
Backquotes ` `
5.
Frequently Asked Questions
5.1.
Can we backslash with a single quote to remove its specialty?
5.2.
Do double quotes support all the special characters?
5.3.
Can we use multiple backslashes in a command?
5.4.
Are these metacharacters predefined?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Linux Shell Quoting Mechanism

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

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.

Linux - Shell Quoting Mechanism

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

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 to Introduction to shell scripting.

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

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.

semicolon command

As you can see in the above image, we are using the ls and cd commands simultaneously with a semicolon; it is working.
 

echo command

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.

shell script 

Now we will try to echo my name is; dhananjay, on the console.

shell script
 

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.

shell script

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.

backslash

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.

backslash

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?

backslash

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.

backslash

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.

single quotes example

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.

single quotes example

Double Quotes “ “

We can use the double-quest mechanism to avoid the single-quote problem and ignore the special characters.

Double Quotes example

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.

Double Quotes example

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.

date command

When putting the date command in the backquotes, you can see it still got executed and displays the date and time.

Backquotes example

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.

Backquotes example

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.

To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Coding Ninjas Studio. Also, you can enroll in our courses and check out the mock test and problems available to you. Please check out our interview experiences and interview bundle for placement preparations.

Happy Coding!

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