The problem is that script receives “test1” in the first place and it cannot possibly know that there was a reference to an empty (undeclared) variable. You have to escape the $
before passing it to the script, like this:
./script.sh "test1\$test2"
Or use single quotes '
like this:
./script.sh 'test1$test2'
In which case bash will not expand variables from that parameter string.