Open In App

Quoting Mechanisms in Linux

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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. 

 

  • $ Variable Substitution or expand the value of Variable.
  • > used for Output Redirection.
  • >> used for Output Redirection to append.
  • < Input redirection.
  • << used for input redirection and is also known as here document.
  • * Match any number of characters, Substitution wildcard for zero or more characters
  • ? Match one character, Substitution wildcard for 1 character
  • [] Match range of characters, Substitution wildcard for any character between brackets
  • `cmd` Replace cmd with the command to execute and will execute that, Substitution wildcard for command execution
  • $(cmd) Replace cmd with the command to execute and will execute that, Substitution wildcard for command execution
  • | Pipe is a Redirection to send the output of one command/program/process to another command/program/process for further processing.
  • ; Command separator is used to execute 2 or more commands with one statement.
  • || OR conditional execution of the commands.
  • && AND conditional execution of the commands.
  • () Groups the command in to one output stream.
  • & executes command in the background and will display the assigned Pid.
  • # to comment something.
  • $ To expand the value of a variable.
  • \ used to escape the interpretation of a character or to prevent that.

 

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-Escape-Character-or-Backslash-in-Linux

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-Single-Quotes-in-Linux

 

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-Double-Quotes-in-Linux

 

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. 

 

The-Back-Quotes

 


Last Updated : 25 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads