Difference between revisions of "Quoting"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
Line 1: Line 1:
 +
Your bash shell understand special characters with special meanings. For example, $var is used to display the variable value. You can enable or disable the meaning of a special character by enclosing them into a single or double quotes. This is also useful to suppress warnings and error messages while write the scripts.
 +
==Quoting==
 
There are three types of quotes  
 
There are three types of quotes  
 +
{| style="cellpadding="20" cellspacing="0" border="1"  width="100%"
 +
! style="background:#ffdead;"| Quote type
 +
! style="background:#ffdead;"| Name
 +
! style="background:#ffdead;"| Meaning
 +
! style="background:#ffdead;"| Example
 +
|-
 +
|"
 +
|Double Quotes
 +
|The double quote ( "quote" ) protects everything enclosed between two double quote marks except $, ', " and \.<br/>Use the double quotes when you want '''variables and command substitution'''.
 +
|<br/><code>echo "$SHELL"<br />echo "Today is $(date)"</code><br />&nbsp;
 +
|-
 +
|'
 +
|Single quotes
 +
|The single quote ( 'quote' ) protects everything enclosed between two single quote marks.<br/>It is used to '''turn off the special meaning''' of all characters.
 +
|<br/><code>echo '$SHELL'<br />echo 'Today is $(date)'</code><br />&nbsp;
 +
|-
 +
|`
 +
|Back quote
 +
|Use back quote ( `command-name` ) to '''execute command''' and replace a command with its output<br/> within the same command-line. However, ''$(command-name)'' is encouraged syntax for substitution as it is recommended<br/> by [[POSIX]] standard and it improves script readability.
 +
|<br/><code>echo "Today is `date`"<br />echo "Today is $(date)"</code><br />&nbsp;
 +
|-
 +
|}
 +
==Backslash==
 +
The backslash ( \ ) alters the special meaning of the ' and " i.e. it will escape or cancel the special meaning of the next character. The following will display filename in double quote:
 +
<source lang="bash">FILE="/etc/resolv.conf"
 +
echo "File is \"$FILE\" "</source>
 +
The following will remove the special meaning of the dollar ( $ ) sign:
 +
<source lang="bash">FILE="/etc/resolv.conf"
 +
echo "File is \$FILE "</source>
 +
  
 
[[Category:Variables and Quoting]]
 
[[Category:Variables and Quoting]]

Revision as of 14:18, 12 September 2009

Your bash shell understand special characters with special meanings. For example, $var is used to display the variable value. You can enable or disable the meaning of a special character by enclosing them into a single or double quotes. This is also useful to suppress warnings and error messages while write the scripts.

Quoting

There are three types of quotes

Quote type Name Meaning Example
" Double Quotes The double quote ( "quote" ) protects everything enclosed between two double quote marks except $, ', " and \.
Use the double quotes when you want variables and command substitution.

echo "$SHELL"
echo "Today is $(date)"

 
' Single quotes The single quote ( 'quote' ) protects everything enclosed between two single quote marks.
It is used to turn off the special meaning of all characters.

echo '$SHELL'
echo 'Today is $(date)'

 
` Back quote Use back quote ( `command-name` ) to execute command and replace a command with its output
within the same command-line. However, $(command-name) is encouraged syntax for substitution as it is recommended
by POSIX standard and it improves script readability.

echo "Today is `date`"
echo "Today is $(date)"

 

Backslash

The backslash ( \ ) alters the special meaning of the ' and " i.e. it will escape or cancel the special meaning of the next character. The following will display filename in double quote:

FILE="/etc/resolv.conf"
echo "File is \"$FILE\" "

The following will remove the special meaning of the dollar ( $ ) sign:

FILE="/etc/resolv.conf"
echo "File is \$FILE "