I have a script containing:
#!/bin/bash
printenv
When I run it from the command line:
env testscript.sh
bash testscript.sh
sh testscript.sh
every time, it outputs SHELL=/bin/bash
. However, when it is run from the cron, it always outputs SHELL=/bin/sh
. Why is this? How can I make cron apply the shebang?
I already checked the cron PATH; it does include /bin.
Best Answer
The shebang is working and cron has nothing to do with that. When a file is executed, if that file's content begins with
#!
, the kernel executes the file specified on the#!
line and passes it the original file as an argument.Your problem is that you seem to believe that
SHELL
in a shell script reflects the shell that is executing the script. This is not the case. In fact, in most contexts,SHELL
means the user's prefered interactive shell, it is meant for applications such as terminal emulator to decide which shell to execute. In cron,SHELL
is the variable that tells cron what program to use to run the crontab entries (the part of the lines after the time indications).Shells do not set the
SHELL
variable unless it is not set when they start.The fact that
SHELL
is/bin/sh
is very probably irrelevant. Your script has a#!/bin/bash
line, so it's executed by bash. If you want to convince yourself, addps $$
in the script to makeps
show information about the shell executing the script.