6

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

closed as unclear what you're asking by countermode, Jeff Schaller, GAD3R, Dmitry Grigoryev, sam Dec 20 '16 at 15:00

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    where are the backticks in your example? Those are single quotes... – Zanna Dec 20 '16 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 – eli Dec 20 '16 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. – Sergiy Kolodyazhnyy Dec 20 '16 at 13:12
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 or ask your own question.