Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to preserve $if below and
would like the following to output

bash is getting in the way by doing
$ substitution

$ sed -e 's:\\$:\\$:g' <<ENDOFFILE  
> $if  


$ tr '$' '\$' <<ENDOFFILE  
> $if  

Is there a way to preserve $keywords
to stdout without explicitly escaping
them out in stdin?

Should I switch to another shell before attempting this?

share|improve this question
Have you tried putting '$if' in single quotes in the text block? – Matt Simmons Aug 3 '10 at 19:32
up vote 6 down vote accepted

I'm not sure what you are trying to do but bash is performing parameter expansion in your here document because the delimiter ENDOFFILE. If you were to change it to 'ENDOFFILE' bash would not expand.

share|improve this answer
Why does this work and the unquoted ENDOFFILE does not? – Dean Toader Aug 3 '10 at 20:07
@Dragos: It's [by-design]. If you look in man bash you'll see that it's a documented feature. – Dennis Williamson Aug 3 '10 at 20:43

Not sure what exactly you are trying to do here, but you can use tr and the octal value of $ which is 044. Experiment with this:

echo 'ab$de' | tr \\044 C

share|improve this answer
Test your recommendation. He's trying to escape a variable via STDIN using string manipulation. You are escaping in the echo. – Warner Aug 3 '10 at 19:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.