9

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 what's going on and how to fix this?

  • 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" – Brian Peterson Oct 17 '13 at 9:35
18

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

  • Thank you. This works. Is there a reason for this? – Amir Afghani May 11 '11 at 19:17
  • 7
    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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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