UNIX / Linux Shell Quoting Mechanisms - Unix/Linux

What are UNIX / Linux Shell Quoting Mechanisms?

In this section, we will converse in specify about the Shell quoting mechanism. We will start by discussing the met characters.

The Met characters

Unix Shell provide different met characters which have special meaning while using them in any Shell Script and causes termination of a word unless quoted.

For instance, Matches with a single character while listing records in a directory and an * matches more than one character. Here is a list of most of the shell particular characters (also called met characters) –

A character may be quoted (i.e., made to stand for itself) by above it with a \.

Example

Following instance shows how to print a * or a? –

Upon execution, you will receive the following result –

Let us now try using a quoted character –

Upon implementation, you will collect the following result –

The $ sign is one of the met characters, so it must be quoted to avoid special handling by the shell –

Upon execution, you will accept the following result –

The following table lists the four forms of quoting −

S.No. Quoting & Description
1
Single quote
All special characters between these quotes lose their special meaning.
2
Double quote
Most special characters between these quotes lose their special meaning with these exceptions −
  • $
  • `
  • \$
  • \'
  • \"
  • \\
3
Backslash
Any character immediately following the backslash loses its special meaning.
4
Back quote
Anything in between back quotes would be treated as a command and would be executed.

The Single Quotes

Consider an echo command that contain many special shell characters –

Put a backslash in front of each special character is dull and makes the line complex to read –

There is a simple way to quote a large group of characters. Put a single quote (') at the beginning and at the end of the string –

Characters within single quotes are quoted just as if a backslash is in front of each character. With this, the echo command displays in a proper way.

If a single quote appear within a string to be output, you should not put the whole string within single quotes instead you should go before that using a backslash (\) as follows –

The Double Quotes

Try to perform the following shell script. This shell script make use of single quote –

Upon execution, you will receive the following result –

This is not what had to be display. It is understandable that single quotes check variable substitution. If you want to substitute variable values and to make reversed commas work as expected, then you would want to put your commands in double quotes as follows –

Upon execution, you will be given the following result –

Double quotes take away the particular meaning of all characters excepting the following −

  • $ for parameter substitution
  • Back quotes for command substitution
  • \$ to enable literal dollar signs
  • \` to enable literal back quotes
  • \" to enable embedded double quotes
  • \\ to enable embedded backslashes
  • All other \ characters are literal (not special)

Characters within single quotes are quoted just as if a backslash is in front of each character. This helps the echo command present correctly.
If a single quote appear within a string to be output, you should not put the whole string within single quotes in its place you should follow that using a backslash (\) as follows –

The Back quotes

Put any Shell command in between back quotes executes the command.

Syntax

Here is the easy syntax to put any Shell command in between back quotes −

Syntax

Example

The date command is execute in the following instance and the formed result is stored in DATA variable.

Upon implementation, you will accept the following result –

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Unix/Linux Topics