Difference between revisions of "Quoting"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
Line 36: Line 36:
 
|}
 
|}
  
==Backslash==
+
==The 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:
 
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"
 
<source lang="bash">FILE="/etc/resolv.conf"
Line 47: Line 47:
 
Sample Outputs:
 
Sample Outputs:
 
<pre>File is $FILE </pre>
 
<pre>File is $FILE </pre>
 
+
===The backslash-escaped characters===
 +
You can use the following backslash-escaped characters. It will get replaced as specified by the ANSI C standard. Quoting directly from bash man page:
 +
<pre>
 +
              \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
 +
</pre>
 +
===Examples===
 +
<source lang="bash">echo -e "\a Ding dong\a"
 +
echo "CIFS path must be \\\\NT-Server-Name\\ShareName"
 +
echo -e "Sr.no\t DVD (price) "
 +
echo -e "1\t Spirited Away (INR.200)"
 +
echo -e "2\t Dragon Ball Z (INR.300)"</source>
 
[[Category:Variables and Quoting]]
 
[[Category:Variables and Quoting]]
 
{{navigation
 
{{navigation
 
|previous=Echo Command|Display the value of shell variables
 
|previous=Echo Command|Display the value of shell variables
 
|next=Export Variables|The export statement}}
 
|next=Export Variables|The export statement}}

Revision as of 16:39, 19 October 2009

← Echo CommandHomeExport Variables →

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)
" The double quote 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 - No
* 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)"

 
' The single quote 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)'

 
\ The Backslash Use backslahs to change the special meaning of the characters or to escape special characters within the text such as quotation marks. You can use \ before dollar sign is used to told to have no special meaning. Disable the meaning of the next character in $PATH (i.e. do not display value of $PATH variable):
echo "Path is \$PATH"
echo "Path is $PATH"
 

The 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 

The backslash-escaped characters

You can use the following backslash-escaped characters. It will get replaced as specified by the ANSI C standard. Quoting directly from bash man page:

              \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

Examples

echo -e "\a Ding dong\a"
echo "CIFS path must be \\\\NT-Server-Name\\ShareName"
echo -e "Sr.no\t DVD (price) "
echo -e "1\t Spirited Away (INR.200)"
echo -e "2\t Dragon Ball Z (INR.300)"

← Echo CommandHomeExport Variables →