Suppose the default shell for my account is zsh but I opened the terminal and fired up bash and executed a script named prac002.sh
, which shell interpreter would be used to execute the script, zsh or bash? Consider the following example:
papagolf@Sierra ~/My Files/My Programs/Learning/Shell % sudo cat /etc/passwd | grep papagolf
[sudo] password for papagolf:
papagolf:x:1000:1001:Rex,,,:/home/papagolf:/usr/bin/zsh
# papagolf's default shell is zsh
papagolf@Sierra ~/My Files/My Programs/Learning/Shell % bash
# I fired up bash. (See that '%' prompt in zsh changes to '$' prompt, indicating bash.)
papagolf@Sierra:~/My Files/My Programs/Learning/Shell$ ./prac002.sh
Enter username : Rex
Rex
# Which interpreter did it just use?
**EDIT : ** Here's the content of the script
papagolf@Sierra ~/My Files/My Programs/Learning/Shell % cat ./prac002.sh
read -p "Enter username : " uname
echo $uname
Best Answer
Because the script does not begin with a
#!
shebang line indicating which interpreter to use, POSIX says that:That phrasing is a little ambiguous, and different shells have different interpretations.
In this case, Bash will run the script using itself. On the other hand, if you ran it from zsh instead, zsh would use
sh
(whatever that is on your system) instead.You can verify that behaviour for this case by adding these lines to the script:
You'll note that, from Bash, the first line outputs your version, while the second never says anything, no matter which shell you use.
/bin/sh
is, say,dash
, then neither line will output anything when the script is executed from zsh or dash./bin/sh
is a link to Bash, you'll see the first line output in all cases./bin/sh
is a different version of Bash than you were using directly, you'll see different output when you run the script from bash directly and from zsh.The
ps -p $$
command from rools's answer will also show useful information about the command the shell used to execute the script.