Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am running this little test script I wrote :

#!/bin/bash

TESTCASE=$@
testHarness <<runTest
$TESTCASE
runTest

from the command line :

./test.sh "1092$212"

but the dollar sign is being stripped out. If I escape it, it works. But I thought the double quotes would protect me from needing to do this. Can someone please help me understand whats going on and how to fix this?

share|improve this question
    
The problem is that it's ambiguous--even for me as a human--what you want here, so the computer has no chance. Perhaps the bash interpreter is trying to get $212. Bash silently fails if you try to expand a variable that doesn't exist, thus the $ gets stripped out. Consider wrapping your variables inside brackets, e.g. "1092${2}12" –  Bepetersn Oct 17 '13 at 9:35

1 Answer 1

up vote 10 down vote accepted

Variables are still expanded inside double quotes. If you want to avoid this behavior you should use single quotes instead.

share|improve this answer
    
Thank you. This works. Is there a reason for this? –  Amir Afghani May 11 '11 at 19:17
4  
Yes. It was designed that way! It's actually quite useful to be able to expand variables inside a quoted string. It's a feature. If you don't want the behavior, use single quotes, that's what they were designed for. –  Caleb May 11 '11 at 19:20

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.