Change Shell

3 posts / 0 new
Last post
#1 Thu, 2015-01-01 01:06
terminator14
  • terminator14's picture
  • Offline
  • Last seen: 3 years 2 months ago
  • Joined: 2015-01-01

Happy Holidays!

I have a bash script (/root/test.sh)t:

#!/bin/bash
echo SHELL=$SHELL > /root/out

Having installed bash with apk, I need cron to run it in bash - not ash, not sh - bash.
I have tried the following options using "crontab -e":

* * * * * /root/test.sh
* * * * * /bin/bash -c "/root/test.sh"

I've even tried moving the script to /root/bin and changing crontab to read:

* * * * * run-parts /root/bin

but /root/out ALWAYS shows:
SHELL=/bin/sh

This may or may not be related, but even without cron, it appears to be impossible to change between shells:

$ echo $SHELL
/bin/ash
$ bash
$ echo $SHELL
/bin/ash
$ /bin/bash
$ echo $SHELL
/bin/ash
$ sh
$ echo $SHELL
/bin/ash
$ /bin/sh
$ echo $SHELL
/bin/ash

The only way I've managed to change shells is by modifying /etc/passwd and logging out, but I can't exactly tell my script
to log out and log back in.

Am I doing something wrong?
PS. I've tried this with Alpine 2.7 and 3.0, with similar results.

Thu, 2015-01-01 12:11
AmatCoder
  • AmatCoder's picture
  • Offline
  • Last seen: 1 year 4 months ago
  • Joined: 2013-10-18

$SHELL variable does not change when you are running another shell.
It tells you which is the default shell (the user's preferred shell stored in passwd file).

If you want to check if you are running bash you can look into the $BASH variable.
$BASH is set when you are running on bash (it shows '/bin/bash') and it is empty on another shells.

Regards.

Thu, 2015-01-01 23:08 (Reply to #2)
terminator14
  • terminator14's picture
  • Offline
  • Last seen: 3 years 2 months ago
  • Joined: 2015-01-01

I assumed that because my PS1 prompt was not changing when switching to different shells without logging out, and the $SHELL variable wasn't either, nothing was happening. Using "echo $0" to check for changes shows different results:

# echo $0
-bash
# sh
# echo $0
sh
# ash
# echo $0
ash

Whatever config file sets PS1 (it looks like it's /etc/profile and /etc/profile.d/*) must only get read during login - not when you switch shells.
My actual script was running without cron fine, but was failing when running with cron - I assumed that the problem was that cron was running it in sh instead of bash - I guess something else is happening with it.
Thanks for helping me clear that up.

Log in or register to post comments