10

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?

1
  • 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" Oct 17, 2013 at 9:35

1 Answer 1

19

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

2
  • Thank you. This works. Is there a reason for this? May 11, 2011 at 19:17
  • 8
    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, 2011 at 19:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .