LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 04-02-2013, 12:50 PM   #1
moraxu
LQ Newbie
 
Registered: Dec 2012
Distribution: Ubuntu
Posts: 15

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


Hello,

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):

Code:
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
\z
EOF # \z
cat <<EOF
\\z
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:

Quote:
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:

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

Code:
$ 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:

Quote:
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:

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

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

Code:
$ echo \z
z
$ echo "\z"
\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:

Code:
$ var=test
$ echo \$var
$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, 01: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: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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:
Code:
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.

Code:
foo=bar

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

#output:
\z
\z
"\z"
"\z"
echo bar
echo $foo
bar
$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:
Code:
$ var=test
$ echo `echo \$var`
test
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 01:55 PM. Reason: formatting & minor fixes
 
1 members found this post helpful.
  


Reply


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 05:13 AM
The sed bash command, not print backslash in Perl azkarashare Programming 8 07-12-2010 07:58 AM
bash: case and command substitution--what gives? David the H. Programming 9 09-03-2009 03:08 AM
bash script command substitution and quoting brian4xp Linux - Software 8 02-05-2008 12:43 PM
Bash Command Substitution dakensta Programming 5 11-30-2006 04:10 PM


All times are GMT -5. The time now is 12:13 PM.

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