Open In App

Quoting Mechanisms in Linux

In Linux Shell, many special characters have their own special meanings. Sometimes they are used to perform an action while other times they are just used as a character, so the quoting mechanism performs this task it makes us use them in whatever way we want to. 

Metacharacters: These are the special characters that are first interpreted by the shell before passing the same to the command. They are also known as shell wildcards. 



 

 



The Escape Character or Backslash

A Backslash(‘\’) is the bash escape character. Any character immediately following the backslash loses its special meaning and any letter following the backslash gains its special meaning. Enter the following commands in the terminal. 

 

echo "Quotation"
echo \"Quotation\"

As it can be seen with the example that double could be not printed without backslash and with backslash as they lost their special meaning, so they were printed. 

 

The Backslash Escaped Characters 
 

\a alert (bell) 
\b backspace 
\e an escape character 
\f form feed 
\n new line 
\r carriage return 
\t horizontal tab 
\v vertical tab 
\\ backslash 
\’ single quote 
\nnn the eight-bit character whose value is the octal value nnn (one to three digits) 
\xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits) 
\cx a control-x character 
 

 

The Single Quotes

Single Quotes(”) are used to preserve the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash. All special characters within the single quotes lose their special meanings. 
Some strings have a lot of special characters so it is hard to use a backslash before every special character. Hence, if we will put the same string within single quotes, most of the special characters will lose their special meanings. Enter the following commands in the terminal. 

 

echo \<Welcome\>\<to\>\<geeksforgeeks\>
echo '<Welcome><to><geeksforgeeks>'

You can be seen from the example that we had to use a lot of backslash in the first statement which made it complex. While with single quotes output was same without using backslash 

 

 

The Double Quotes

Double Quotes(“”) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘\’, and when history expansion is enabled, ‘!’. The backslash retains its special meaning only when followed by one of the following characters: ‘$’, ‘`’, ‘”’, ‘\’, or newline. Enter the following commands in the terminal. 

 

name=geeksforgeeks
echo '$name'
echo "$name"

You can be seen that in the case of single quotes the value of the variable was not printed while in the case of double quotes the value of the variable was printed. 

 

 

The Back Quotes

Back Quotes(“) are used to execute a command. Anything enclosed between them will be treated as a command and would be executed. Open the terminal and execute the following commands. 

 

hostname=`hostname`
echo $hostname

You can be seen from the example the hostname command gets executed and the name gets stored in the variable. 

 

 

Article Tags :