Changes

Jump to navigation Jump to search
2,890 bytes added ,  22:50, 29 March 2016
m
Text replacement - "</source>" to "</syntaxhighlight>"
|next=Export Variables|The export statement}}
Your bash shell understand understands special characters with special meanings. For example, $var is used to display expand the variable value. Bash expands variables and [[wildcards]], for example:<source syntaxhighlight lang="bash">echo "$PATH"echo "$PS1"echo /etc/*.conf</sourcesyntaxhighlight>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 in single or double quotes. This is also useful to suppress warnings and error messages while writing the shell scripts. <source syntaxhighlight lang="bash">echo "Path is $PATH" ## $PATH will be expanded</sourcesyntaxhighlight>
OR
<source syntaxhighlight lang="bash">echo 'I want to print $PATH'## PATH will not be expanded</sourcesyntaxhighlight>
==Quoting==
There are three types of quotes :
{| style="cellpadding="20" cellspacing="0" border="1" width="100%"
! style="background:#ffdead;"| Quote type
|-
|"
|Double QuotesThe 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'''.<br/>* '''Variable''' - Yes<br/>* '''Wildcards''' - No<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;
|-
|'
|Single quotes 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.<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;
|-
|`\|Back quote The Backslash|Use back quote ( `command-name` ) backslash to change the special meaning of the characters or to '''execute command''' and replace a command with its output escape special characters within the same command-line. However, ''$(command-name)'' is encouraged syntax for substitution text such as it is recommended by [[POSIX]] standard and it improves script readabilityquotation marks.<br/>* '''Variable''' - Yes<br/>* '''Wildcards''' - Yes<br/>* '''Command substitution''' - yes|The [[date command]] is executed and its output is substituted back You can use \ before dollar sign to tell the shell to [[echo command]]have no special meaning. Disable the meaning of the next character in $PATH (i.e.do not display value of $PATH variable):<br/><code>echo "Today Path is `date`\$PATH"<br />echo "Today Path is $(date)"<br/>echo "$(ls /etc/*.conf)"<br/>FILES=/etc/resolv.conf<br/>echo "$(cat $FILE)PATH"</code><br />&nbsp;
|-
|}
==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:
<source syntaxhighlight lang="bash">FILE="/etc/resolv.conf"echo "File is \"$FILE\" "</sourcesyntaxhighlight>
Sample Outputs:
<pre>File is "/etc/resolv.conf"</pre>
The following will remove the special meaning of the dollar ( $ ) sign:
<source syntaxhighlight lang="bash">FILE="/etc/resolv.conf"echo "File is \$FILE "</sourcesyntaxhighlight>
Sample Outputs:
<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>
Use -e option of echo command to enable interpretation of backslash escapes.
 
===Examples===
<syntaxhighlight lang="bash" >echo "Pizza bill \$22.5"
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)"</syntaxhighlight>
The special parameters * and @ have special meaning when in double quotes, but you can disable them with the backslash:
<syntaxhighlight lang="bash" >echo "*"
echo "\*"
echo "\@"</syntaxhighlight>
====Continue command on next line====
You can use the backslash (\) as last character on line to continue command on next line:
<syntaxhighlight lang="bash" >echo "A monkey-tailed boy named Goku is found by an old martial \
>arts expert who raises him as his grandson. One day Goku meets a \
>girl named Bulma and together they go on a quest to retrieve the seven Dragon Balls"</syntaxhighlight>
You can also use the backslash while writing program or [[Writing_your_first_shell_function|function]]:
<syntaxhighlight lang="bash" ># Purpose: clean /tmp/$domain ?
check_temp_clean() {
[ "$SERVER_MODE" = "daemon" ] || return 1
[ "$SERVER_MODE" = "init" ] && return 0
# note use of the backslash character to continue command on next line
[ "$SERVER_MODE" = "clean" \
-a -e /usr/local/etc/nixcraft/lighttpd/disk_cache.init ] && return 0
return 1
}</syntaxhighlight>
 
====Protecting command line arguments====
Type the following command to find out all c program file (*.c):
<syntaxhighlight lang="bash" >find $HOME -name *.c</syntaxhighlight>
In the above example, the *.c is expanded by the bash shell. It will try to match all filename ending with .c in the /home directory (current user's home directory) such as main.c, lib1.c, lib2.c, ssh.c, auth.c etc. You can escape the wild card using the backslash as the escape character:
<syntaxhighlight lang="bash" >find $HOME -name \*.c
find $HOME -name \*main.c
find /nas01/apps/admin -iname \*py </syntaxhighlight>
You can also use the double quote
<syntaxhighlight lang="bash" >find $HOME -name "*.c"</syntaxhighlight>
[[Category:Variables and Quoting]]

Navigation menu