Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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. )

share|improve this question

2 Answers 2

up vote 30 down vote accepted

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 ~/.profile when logging in through xdm or similar), and idioms such as ssh 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
share|improve this answer
8  
Excellent answer! stackexchange needs a Send A Beer button: this would be a time I'd use it. –  iconoclast Aug 30 '12 at 13:39

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.

share|improve this answer
3  
you could of course change $SHELL fairly easily by adding a line to do so before the exec. –  derobert Aug 29 '12 at 21:35
2  
If you want the new shell to also be a login shell you should probably exec bash -l. –  jw013 Aug 29 '12 at 21:48
1  
One last comment: type is recommended over which. –  jw013 Aug 29 '12 at 22:06
1  
I don't get it! Won't bash also read .profile thus generating an infinite loop? I agree that something along the lines of exec bash is what you want, but you need to make sure you do it only to your login ksh so some if-statements are required! –  Bananguin Aug 30 '12 at 6:21
1  
@user1129682 not if there is ~/.bash_profile or ~/.bash_login –  Ulrich Dangel Aug 30 '12 at 7:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.