Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

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

2 Answers

up vote 5 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
1  
@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
add comment

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
add comment

Your Answer

 
discard

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.