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 chracters
- ? 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 Backslash Escaped Characters
\a alert (bell)
\e an escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\’ 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.
- Difference Between Arch Linux and Kali Linux
- Linux Virtualization : Linux Containers (lxc)
- Why Linux is Better?
- Wikit in Linux
- Linux Commands
- while command in Linux with example
- Run Levels in Linux
- Different Shells in Linux
- Linux | Nmon
- who command in Linux
- SED command in Linux | Set 2
- Fun Commands in Linux
- du Command in LINUX
- od command in Linux with example
- The Linux Kernel
- 10 Best Linux VPS Hosting
- Permissions in Linux
- 'dd' command in Linux
- Linux vs Unix
- Filters in Linux
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.