89

For example, $PATH and $HOME

When i type echo $PATH it returns my $PATH, but i want to echo the word $PATH and not what the actual variable stands for, echo "$PATH" doesn't work either.

6
  • yes use single quote like : echo '$PATH'. for more information about quoting, visit this and this
    – Pandya
    Commented Oct 16, 2014 at 12:24
  • @don only saw a dupe on serverfault. My bad. Commented Oct 16, 2014 at 12:24
  • Possible duplicate of How to use a special character as a normal one? Commented Sep 14, 2016 at 10:42
  • @StéphaneChazelas That question is almost two years newer than this one. Commented Sep 14, 2016 at 12:28
  • 1
    @DisplayName, yes but is intended as a "canonical" question to cover all those "how to I display special character X". (I'm not sure I like the idea of those "canonical" cover-it-all answers being used to close specific questions though which is why I just added it as a comment instead of a close vote). Commented Sep 14, 2016 at 12:40

4 Answers 4

104

You just need to escape the dollar $.:

echo \$PATH
$PATH

Or surround it in single quotes:

echo '$PATH'
$PATH

This will ensure the word is not interpreted by the shell.

5
  • 1
    Thanks, it didn't say anything about echo -e on the man page. Commented Oct 16, 2014 at 11:35
  • The -e flag is used to expand, e.g, \t to a literal tab character. It is unrelated to escaping the dollar sign to prevent parameter expansion.
    – chepner
    Commented Oct 16, 2014 at 12:20
  • 2
    This doesn't work when directing to a file. echo '\$PATH' >> output.txt results in "/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/vc/bin" being written to the file, not "$PATH".
    – Cerin
    Commented Dec 17, 2015 at 4:03
  • 2
    Very interesting, if you are using single quotes and an escaped character, then that should just echo \$PATH. What OS / package is your echo comand sourced from? Commented Dec 22, 2015 at 15:55
  • @Cerin for you @stéphane answer worked echo '\044*' > first.txt it will output $* in first.txt
    – maq
    Commented Jul 26, 2018 at 17:31
28

$ in the syntax of most shells is a very special character. If we only look at Bourne-like shells it is used to introduce:

  1. parameter expansions as in $#, $var, ${foo:-bar}
  2. command substitution as in $(logname) (also ${ logname;} in some shells to avoid the subshell)
  3. arithmetic expansion as in $((1+1)) (also $[1+1] in some shells).
  4. other forms of quoting in some shells like $'\n' or $"localized string".

Except for the last case, those expansions/substitutions still occur inside double quotes (and inside $"..." in bash or ksh93)

To stop that $ from being treated specially, you need to quote it with either:

  • single quotes: echo '$PATH' or echo '$'PATH. The one you'd generally want to use as it escapes all characters (except itself) in most shells.
  • blackslash: echo \$PATH. Also inside double quotes: echo "\$PATH"
  • in some shells, some other quoting operator: echo $'$PATH', echo $'\44PATH' (assuming an ASCII compatible charset), echo $'\u0024'

Or make sure it's not part of a valid form of expansion as listed above:

  • echo "$"PATH
  • echo $""PATH, echo $"PATH". Those should be avoided because some shells like ksh93 and bash support the $"..." form of quotes.
  • echo $``PATH. No reason why you'd want to use that one except to show that it's possible.
  • echo "${$+$}PATH" and other convoluted ones...

Or you could output that $ another way:

  • with a Unix-compliant echo: echo '\044PATH' (some other echos need echo -e '\044PATH')
  • printf '\44PATH\n'
  • cat << \EOF
    $PATH
    EOF

Note how those backslashes need to be quoted as they also are special to the shell obviously.

Should hopefully be clear by now that although there are many ways to do it, there's no good reason you'd want to use anything else but echo '$PATH' here.

2

Note that literal $ inside double quotes can be a problem. It can be done by producing the $ outside the double quotes, like this:

echo "My terminal is $term, first file found in /dev is </dev/`ls /dev | head -n 1`>, the
dollar symbol is '"`echo '$'`"', and a newspaper costs "`echo '$'`"2.50."

or like this:

echo "$term is the value of "'$term'

OUTPUT:

My terminal is xterm, first file found in /dev is </dev/acpi>, the dollar symbol
is '$', and a newspaper costs $2.50.

xterm is the value of $term
2
  • 1
    Or echo "$term is the value of \$term" as already explained in another answer. Or printf '%s is the value of $term\n' "$term" as anyway one shouldn't use echo for arbitrary data. Commented Jan 9, 2018 at 13:53
  • In Debian $term is empty use $TERM instead.
    – EsmaeelE
    Commented Jan 28 at 7:37
-1

Use:

echo ''\$PATH'' >> output.txt 

This will work.

4
  • 3
    please add a comment when down-voting
    – andreas-h
    Commented May 17, 2016 at 14:55
  • 2
    Or ec""h'''o''' ''""''\$P''A""TH'' Commented Sep 14, 2016 at 10:00
  • 1
    I'm not sure what is being attempted by this answer. The answer is to use either \$ or single quotes. echo '$PATH' will print $PATH echo '\$PATH' will print \$PATH echo "\$PATH" will print $PATH echo ''\$PATH'' will technically do the right thing, but the two null strings serve no purpose.
    – bobpaul
    Commented Apr 6, 2017 at 16:42
  • I dont understand why this answer is downvoted. It is perfectly working fine.
    – nomadSK25
    Commented Sep 12, 2022 at 2:31

You must log in to answer this question.

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