2

I want to echo/printf a dollar sign into a file - but echo $ > myfile or such does not work (not surprising).

I tried escaping it like echo \$ > myfile but it is also not working.

What is the correct way to perform it?

I'm working with TCSH on SunOs.

1
  • 1
    The escaped version worked on my site (on Linux as well as on SunOS).
    – bmk
    May 9, 2011 at 8:53

2 Answers 2

3

You can use printf and single quotes to avoid variable expansion:

printf '$' > myfile
2
  • Erm, why would you choose printf to echo a static string? May 9, 2011 at 11:16
  • 3
    @jgoldschrafe: Because I'm sure the Solaris printf command will be used in that case so I can avoid whatever oddity tcsh echo builtin might introduce.
    – jlliagre
    May 9, 2011 at 12:15
2

From this link, you can use the \x24 method, where 24 is the hex for the dollar sign. Just that this structure doesn't work 100% with string using the double quote, so you probably want to use a single quote. So this statement

echo "Foo "$'\x24'" bar" >> somefile

will create somefile with the content

Foo $ bar

Note: tcsh (name dropped in the original question) does not support dollar-quote literals (like $'\x24'). Nor does dash. It is not a POSIX standardized feature.

You must log in to answer this question.

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