Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

unix
linux

What are quoting mechanisms in Unix/Linux?

Sheza Munir

Linux shell allows the use of characters that have special meanings in the Linux shell. Here, we look at the following quoting mechanisms we can use via special characters:

  • Escape character or backslash
  • Single quotes
  • Double quotes
  • Back quotes

Escape characters

Escape characters are initialized with a backslash \. When a character follows a \, it has a special meaning. Common escape characters include:

Escape character

Meaning

\n

This adds a new line.

\e

This is an equivalent to ESC.

\b

This means backspace.

\t

This adds a horizontal tab.

\v

This adds a vertical tab.

\f

This represents form feed.

\r

This is equivalent to carriage return.

\\

This means a literal backslash.

\'

This represents a single quote.

\"

This represents a double quote.

Note: Instead of adding a new line in the CLI, you can use \n.

Single quotes

Single quotes help the user preserve the literal value of the input in the form of a string. If there are any special characters within the single quotes, they are treated as normal characters and the special role does not apply. However, there should be no single quotes within the single quotes. It is best to use single quotes if the input string has a lot of special characters.

Example

It is easier to write 'This\ is\ Educative', than This \\ is \\ Educative.

Double quotes

Double quotes help the user preserve the literal value of the input in the form of a string, except for some specific characters. The characters that will stay special are , \$, \, \', \", ! and the backtick “`”.

The backslashes in the input only have special meaning if they are followed by one of the mentioned special characters.

Example

With single quotes, '\$Educative', stays a literal \$Educative. However, with double quotes, "\$Educative" becomes $Educative.

Back quotes

Back quotes execute commands. The string inside the backquotes is treated as a separate command. They tell the shell to take the string between the back quotes as a system command and execute its output. You can then use the output in any way you like.

Example

Enter the following command in the bash shell: $ echo `date`

The shell will execute the string inside the back quotes as a separate command and give the following output:
Sun May 16 16:40:19 GMT 2021

RELATED TAGS

unix
linux

CONTRIBUTOR

Sheza Munir
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring