This is an extension to Which shell interpreter runs a script with no shebang?
I was testing my understanding of what interpreter is used, and found some odd behaviour when running a script without a hashbang but as sudo.
Script is as follows:
echo "I am running shell $SHELL"
foo=$'dollar'
echo "run with sudo to see the magic dollar sign <$foo>"
When I ran this script as my normal user, all fine, outputs:
I am running shell /bin/bash
run with sudo to see the magic dollar sign <dollar>
But when I run with sudo I get output
I am running shell /bin/bash
run with sudo to see the magic dollar sign <$dollar>
So without sudo, foo expands to "dollar", but with sudo, the $'' evaluation is ignored and it becomes "$dollar"
I also now understand that $'' not POSIX compliant, and so wouldn't necessarily work if running the script except under bash. And confirm that sudo bash myscript.sh
returns the expect "dollar" output.
However, in any of these scenarious, $SHELL implies the script is running under bash. What is going on?
PS - my /bin/sh links to dash - I can only think it has something to do with this