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.
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.
echo -e
on the man page.
Commented
Oct 16, 2014 at 11:35
-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.
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".
echo '\044*' > first.txt
it will output $* in first.txt
$
in the syntax of most shells is a very special character. If we only look at Bourne-like shells it is used to introduce:
$#
, $var
, ${foo:-bar}
$(logname)
(also ${ logname;}
in some shells to avoid the subshell)$((1+1))
(also $[1+1]
in some shells).$'\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:
echo '$PATH'
or echo '$'PATH
. The one you'd generally want to use as it escapes all characters (except itself) in most shells.echo \$PATH
. Also inside double quotes: echo "\$PATH"
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"
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:
echo
: echo '\044PATH'
(some other echo
s 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.
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
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
Use:
echo ''\$PATH'' >> output.txt
This will work.
echo '$PATH'
. for more information about quoting, visit this and this