1

Invoking a script using sudo ignores the shebang and runs the script in a different shell. To test, I created a script (test.sh) containing:

#/bin/bash
echo "BASH is: $BASH"
echo "actual shell is: `readlink /proc/$$/exe`"

First, I invoke the script without sudo:

$ ./test.sh
BASH is: /bin/bash
actual shell is: /bin/bash

Then, I invoke the script with sudo:

$ sudo ./test.sh
BASH is:
actual shell is: /bin/dash

I would not have expected this. Is this normal behavior?

Note: I'm using Ubuntu (14.04), where the default shell /bin/sh is a symlink to dash.

2 Answers 2

3

Your shebang isn't a shebang. It's just a she, missing the bang:

#!/bin/bash  

Corrected example:

$ ./test.sh
BASH is: /bin/bash
actual shell is: /bin/bash
$ sudo ./test.sh
BASH is: /bin/bash
actual shell is: /bin/bash
$ cat ./test.sh 
#!/bin/bash
echo "BASH is: $BASH"
echo "actual shell is: `readlink /proc/$$/exe`"
0
3

Your example omits the ! character, e.g., you should have #!/bin/bash

Without a valid hashbang line, you will get the default shell, which is dash.

0

You must log in to answer this question.

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