Difference between revisions of "Quoting"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
Line 17: Line 17:
 
|"
 
|"
 
|Double Quotes
 
|Double Quotes
|The double quote ( "quote" ) protects everything enclosed between two double quote marks except $, ', " and \.Use the double quotes when you want only '''variables and command substitution'''.
+
|The double quote ( "quote" ) protects everything enclosed between two double quote marks except $, ', " and \.Use the double quotes when you want only '''variables and command substitution'''.<br/>* '''Variable''' - Yes<br/>* '''Wildcards''' - Yes<br/>* '''Command substitution''' - yes
 
|The double quotes allowes to print the value of $SHELL variable, disables the meaning of [[wildcards]], and finally allows command substitution.<br/><code>echo "$SHELL"<br/>echo "/etc/*.conf"<br />echo "Today is $(date)"</code><br />&nbsp;
 
|The double quotes allowes to print the value of $SHELL variable, disables the meaning of [[wildcards]], and finally allows command substitution.<br/><code>echo "$SHELL"<br/>echo "/etc/*.conf"<br />echo "Today is $(date)"</code><br />&nbsp;
 
|-
 
|-
 
|'
 
|'
 
|Single quotes
 
|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.
+
|The single quote ( 'quote' ) protects everything enclosed between two single quote marks. It is used to '''turn off the special meaning''' of all characters.<br/>* '''Variable''' - No<br/>* '''Wildcards''' - No<br/>* '''Command substitution''' - No
 
| The single quotes prevents displaying variable $SHELL value, disabled the meaning of [[wildcards]] /etc/*.conf, and finally command substitution ($date) itself. <br/><code>echo '$SHELL'<br/>echo '/etc/*.conf'<br />echo 'Today is $(date)'</code><br />&nbsp;
 
| The single quotes prevents displaying variable $SHELL value, disabled the meaning of [[wildcards]] /etc/*.conf, and finally command substitution ($date) itself. <br/><code>echo '$SHELL'<br/>echo '/etc/*.conf'<br />echo 'Today is $(date)'</code><br />&nbsp;
 
|-
 
|-
 
|`
 
|`
 
|Back quote
 
|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.
+
|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.<br/>* '''Variable''' - Yes<br/>* '''Wildcards''' - Yes<br/>* '''Command substitution''' - yes
|The [[date command]] is executed and its output is substituted back to [[echo command]].<br/><code>echo "Today is `date`"<br />echo "Today is $(date)"</code><br />&nbsp;
+
|The [[date command]] is executed and its output is substituted back to [[echo command]].<br/><code>echo "Today is `date`"<br />echo "Today is $(date)"<br/>echo "$(ls /etc/*.conf)"<br/>FILES=/etc/resolv.conf<br/>echo "$(cat $FILE)"</code><br />&nbsp;
 
|-
 
|-
 
|}
 
|}

Revision as of 14:48, 12 September 2009

Your bash shell understand special characters with special meanings. For example, $var is used to display the variable value. Bash expands variables and wildcards, for example:

echo $PATH
echo $PS1
echo /etc/*.conf

However, sometime you do not wish to use variables or wildcards. For example, do not print value of $PATH, but just print $PATH on screen as a word. 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 writing the shell scripts.

echo "Path is $PATH"

OR

echo 'I want to print $PATH'

Quoting

There are three types of quotes

Quote type Name Meaning Example (type at shell prompt)
" Double Quotes The double quote ( "quote" ) protects everything enclosed between two double quote marks except $, ', " and \.Use the double quotes when you want only variables and command substitution.
* Variable - Yes
* Wildcards - Yes
* Command substitution - yes
The double quotes allowes to print the value of $SHELL variable, disables the meaning of wildcards, and finally allows command substitution.
echo "$SHELL"
echo "/etc/*.conf"
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.
* Variable - No
* Wildcards - No
* Command substitution - No
The single quotes prevents displaying variable $SHELL value, disabled the meaning of wildcards /etc/*.conf, and finally command substitution ($date) itself.
echo '$SHELL'
echo '/etc/*.conf'
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.
* Variable - Yes
* Wildcards - Yes
* Command substitution - yes
The date command is executed and its output is substituted back to echo command.
echo "Today is `date`"
echo "Today is $(date)"
echo "$(ls /etc/*.conf)"
FILES=/etc/resolv.conf
echo "$(cat $FILE)"

 

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\" "

Sample Outputs:

File is "/etc/resolv.conf"

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

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

Sample Outputs:

File is $FILE