The unix sysadmin where I’m working is reluctant to give me access to change my login shell from ksh to bash. He has given various excuses, the funniest being that since they write all their scripts for ksh they won’t work if I try to run them. I don’t know where he gets these ideas, but since I can’t convince him, is there any alternative that I have?
(chsh is installed on these machines, but we use public/private keypairs for logging in, and I don’t have any password, so when chsh prompts me for a password I have nothing to give it. )
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
When you log in, the file ~/.profile is read by the login shell (ksh for you). You can instruct that login shell to replace itself by bash. You should take some precautions:
- Only replace the login shell if it’s interactive. This is important: otherwise, logging in in graphic mode may not work (this is system-dependent: some but not all systems read
~/.profilewhen logging in through xdm or similar), and idioms such asssh foo '. ~/.profile; mycommand'will fail. - Check that bash is available, so that you can still log in if the executable isn’t there for some reason.
You have a choice whether to run bash as a login shell or not. The only major difference in making it a login shell is that it’ll load ~/.bash_profile or ~/.profile. So if you run bash as login shell, be very careful to have a ~/.bash_profile or take care not to execute bash recursively from ~/.profile. There is no real advantage of having ~/.profile executed by bash rather than ksh, so I’d recommend not doing it.
Also set the SHELL environment variable to bash, so that programs such as terminal emulators will invoke that shell.
Here’s code to switch to bash. Put it at the end of ~/.profile.
case $- in
*i*)
# Interactive session. Try switching to bash.
if [ -z "$BASH" ]; then # do nothing if running under bash already
bash=$(command -v bash)
if [ -x "$bash" ]; then
export SHELL="$bash"
exec "$bash"
fi
fi
esac
Method 2
This is slightly kludgey, but you can cause bash to be the shell you’re using upon login by creating a .profile file in your home directory, containing
SHELL=`type -P bash` exec bash -l
This will cause the ksh session to be replaced with a bash session. You won’t have to type exit (or ^D) twice, as you would if you manually started a new bash session every time you logged in. And typing
echo $SHELL
will even return the path to bash.
Method 3
Giles’ answer should have the -l flag added when executing bash, so that any login profile scripts will be sourced in the new bash shell. (For example anything in /etc/profile.d/ on RHEL). The script should then be:
case $- in
*i*)
# Interactive session. Try switching to bash.
if [ -z "$BASH" ]; then # do nothing if running under bash already
bash=$(command -v bash)
if [ -x "$bash" ]; then
export SHELL="$bash"
exec "$bash" -l
fi
fi
esac
Method 4
An answer for csh/tcsh inspired by the top voted one by @Gilles is
if ( ! $?BASH ) then
setenv BASH `command -v bash`
endif
if ( $?prompt ) then
if ( -x "$BASH" ) then
exec "$BASH"
endif
endif
Method 5
Solaris:
usermod -s /bin/bash root
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0