How can I echo dollar signs?

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.


$ 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.

Tags:

Shell

Quoting