Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-02-2013, 11:50 AM   #1
LQ Newbie
Registered: Dec 2012
Distribution: Ubuntu
Posts: 15

Rep: Reputation: Disabled
Meaning of backslash in bash within here document and command substitution


I've questions regarding somewhat obscure rules on the meaning of backslash in bash shell in different contexts.

The following snippet is taken from here (p. 54):

The behavior of \ depends on whether it is escaped, strong-quoted, weak-quoted, or appearing within
command substitution or a here document.

# Simple escaping and quoting
echo \z # z
echo \\z # \z
echo '\z' # \z
echo '\\z' # \\z
echo "\z" # \z
echo "\\z" # \z

# Command substitution
echo `echo \z` # z
echo `echo \\z` # z
echo `echo \\\z` # \z
echo `echo \\\\z` # \z
echo `echo \\\\\\z` # \z
echo `echo \\\\\\\z` # \\z
echo `echo "\z"` # \z
echo `echo "\\z"` # \z

# Here document
cat <<EOF
EOF # \z
cat <<EOF
EOF # \z
Simple escaping is rather obvious to me, but I have a question regarding old-syntax command substitution and here document.

1) From the bash manual:

When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or \. The first backquote not preceded by a backslash terminates the command substitution.
When backslash retains its literal meaning, it should appear as itself, right? Then why in this case:

echo `echo \z`
the output is z instead of \z? Same here:

$ var=test
$ echo `echo \$var`
echo outputs test when it supposed to write out the variable name var. Why is this happening?

2) Again, from the bash manual:

Here Documents
If word [EOF in the example below] is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion. In the latter case, the character sequence \newline is ignored, and \ must be used to quote the characters \, $, and `.
Since backslash must be used to quote another one, then why here:

cat <<EOF
cat outputs \z instead of just z?
Old 04-02-2013, 12:45 PM   #2
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,470

Rep: Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858Reputation: 2858
You need to have a look at the commands you are issuing.

$ echo \z
$ echo "\z"
Since the output of the echo is what is dealing with the backslash, passing it as output of a substitution means it is already removed.

Your second example with the variable follows the same rule:

$ var=test
$ echo \$var
As '$var' is now passed to the next echo it merely outputs the value of the variable.

As for the here document, I am not sure what your issue is? None of, parameter expansion, command substitution, and arithmetic expansion, are relevant to '\z', so it is output as is.
Old 04-02-2013, 12:51 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
In regular, unquoted situations, every character with a backslash preceding it has it's backslash processed, whether it needs it or not.

But inside quotes or a heredoc, only the few listed characters are treated specially. Backslashes preceding all other regular characters will remain literal.

command + # output + # explanation:
echo \z     # z         #backslash interpreted.
echo \\z    # \z        #2 backlashes become one.
echo "\z"   # \z        #backslash not intepreted, not preceding a special character
echo "\\z"  # \z        #2 backslashes become one. "\" precedes a special character, another '\'.
A heredoc does not process quotemarks, but it does process "$" and "`", so substitutions still happen. It also processes backslashes if they are in front of the reserved characters, allowing you to escape them if needed. All other characters are ignored just as in the quoted situation above.


cat <<EOF
echo $foo
echo \$foo
$( echo $foo )
$( echo \$foo )

echo bar
echo $foo
Note that if the delimiting word (the opening EOF) is quoted or backslashed, it won't process anything. The entire text will become literal.

As for this:
$ var=test
$ echo `echo \$var`
The way `..` processes backslashes appears to be rather confusing overall. See here. It appears to be doing some kind of double-expansion, in fact.

$(..), on the other hand, simply processes everything it contains as a regular command subshell. Just another reason not to use backticks!

Last edited by David the H.; 04-02-2013 at 12:55 PM. Reason: formatting & minor fixes
1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash - command substitution kopert Programming 8 09-18-2012 04:13 AM
The sed bash command, not print backslash in Perl azkarashare Programming 8 07-12-2010 06:58 AM
bash: case and command substitution--what gives? David the H. Programming 9 09-03-2009 02:08 AM
bash script command substitution and quoting brian4xp Linux - Software 8 02-05-2008 11:43 AM
Bash Command Substitution dakensta Programming 5 11-30-2006 03:10 PM

All times are GMT -5. The time now is 10:33 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration