7

This is an excerpt from bash manual:

When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or \

But backticks treat $ and \$ in the same way as suggested by the output of the following commands:

Command        Output
echo '$PWD'    $PWD
echo '\$PWD'   \$PWD
3
  • 6
    where are the backticks in your example? Those are single quotes...
    – Zanna
    Dec 20, 2016 at 12:35
  • additionally you may want to use echo $(echo $PWD) (or choose the backtick syntax `echo `echo $PWD` ) and to just print the contents of the variable leave the quotes: echo $PWD
    – MacMartin
    Dec 20, 2016 at 12:49
  • Aside form Nikhil's example , I am struggling with backquotes here as well. As far as I understand the manual, var=`echo \USER` should print out "\USER" , but it's not there. Dec 20, 2016 at 13:12

2 Answers 2

12

I am new to stackexchange and to Linux also. Thanks in advance.

Welcome to both!

There are no backticks in your example, those are single quotes: '' Backticks looks like this: ``

Also, I would suggest that you simply don't use them (the backticks that is)! It is better to use this syntax for command substitution: $(<command>)

Read about why here.

Happy hacking!

3

The way I see it is that there's miscommunication: what bash manual authors call literal value isn't what we as regular users may understand as literal value. Also, there's two things at play. First, backquotes serve for command substitution, and second the "literal meaning of backslash". Lets start with the last one.

Quote from bash's manual, QUOTING section:

A  non-quoted backslash (\) is the escape character.
It preserves the literal value of the next character that
follows, with the exception of <newline>. 

What's not obvious here is that authors are talking about the actual backslash, the text. Single backslash itself stands for escape character, but what they're talking about in the link you provided is the double back slash. Take a look at this:

bash-4.3$ var=`echo \ `
bash-4.3$ echo $var

bash-4.3$ var=`echo \\ `
bash-4.3$ echo $var

bash-4.3$ var=`echo \\$ `
bash-4.3$ echo $var
$
bash-4.3$ var=`echo \\\ `
bash-4.3$ echo $var
\

It's difficult to wrap your head around this, but think of it this way: you have to escape the escape character first.

Not the answer you're looking for? Browse other questions tagged .