up vote 3 down vote favorite
share [g+] share [fb]

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

bash is getting in the way by doing
$ substitution

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

or

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

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?

Thanks,
Dragos

link|improve this question
1  
Have you tried putting '$if' in single quotes in the text block? – Matt Simmons Aug 3 '10 at 19:32
feedback

2 Answers

up vote 3 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.

link|improve this answer
Why does this work and the unquoted ENDOFFILE does not? – Dragos 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
feedback

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

link|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
feedback

Your Answer

 
or
required, but never shown

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