Re: Weird shell script behavior in a cron job
On 31 August 2017 at 04:32, James H. H. Lampert
> I added a line to echo $SHELL to my debugging log file, and
> that was it: if I ran it from cron, $SHELL was /bin/sh; if I ran it from a
> command line, $SHELL was /bin/bash.
Be careful to correctly understand the purpose of the SHELL environment
variable. It does *not* report the value of the running shell. Here is a
demonstration, using an interactive terminal on jessie, where the shell 'dash'
is told to output the value of SHELL:
$ dash -c 'echo "shell is $SHELL"'
shell is /bin/bash
The SHELL variable inherited by child process of an interactive login is set by
login (see 'man 1 login') to the value read from /etc/passwd. You can redefine
it if you wish. My login shell is /bin/bash, so I see the above output.
The cron SHELL variable is set by cron, see 'man 5 crontab'.
The purpose of the SHELL variable is to offer information to child processes
which executable you want them to run whenever they create a child shell,
if they are polite enough to obey it.
What you saw is that cron sets SHELL to /bin/sh, and your user login
sets it to /bin/bash.
My example above shows that SHELL does not confirm which actual shell
is running, although in general they will be the same unless something else
has caused them to be different, which can easily happen as shown by the