If I specify my shell as /bin/bash in a script, then the EUID of root is 0. If I don't, and the script runs in my default shell (also /bin/bash), the EUID of root is an empty string! I'm new to scripting and thought there was no difference as long as bash ran the show.
The code I'm running checks if the program is run by root, and restarts the program as sudo if not.
#!/bin/bash
echo euid = $EUID
echo shell = $SHELL
if [ $EUID -ne 0 ]; then
sudo "$0"
exit $?
fi
When run, I see
euid = 1000
shell = /bin/bash
euid = 0
shell = /bin/bash
But if I remove the she-bang line, I get
euid = 1000
shell = /bin/bash
euid =
shell = /bin/bash
./test.sh: 4: [: -ne: unexpected operator
The script runs in the same shell all four times, so why does it act differently when sudo calls it without specifying /bin/bash?
I'm running Ubuntu 14.04 if that matters.
Thanks in advance!
By removing the shebang line, you are calling Ubuntu default shell, which is Dash, not Bash.
Dash does not define $EUID
, leading to an empty assignment. In fact, $EUID
is one of many enhancements brought by Bash over the POSIX standard. Instead, Dash is meant to be as small, fast and lightweight as possible, and implements only the minimal set of features required by the standard.
You assert that it runs in the same shell all four times, but in fact it doesn't. The environment variable $SHELL
does not refer to the shell running the current process; it refers to your account's login shell, defined in /etc/passwd
or equivalent.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.