Login shells

How can I change my login shell so that bash (or tcsh, or whatever) runs automatically at startup?

First of all open a separate terminal window with your current shell and leave it open. This will allow you to undo anything that goes wrong.

Actually changing your login shell

Try:

        chsh -s /bin/bash [username]

Or on clusters with settings shared using NIS:

        ypchsh -s /bin/bash [username]

If that doesn’t work it might be because your Unix uses a different command, or that it can’t be done by users. You can either ask your sysadmin to change it for you, or apply the hack in the next section. Note if you are using older versions of MacOSX you must change your shell in the netinfo manager (chsh always existed but used to be broken). I have no idea how to deal with Suns, I’ve always just done the below.

A hacky alternative that also works

Edit your (unwanted) shell’s configuration file to load the shell you do want. If you are editing .cshrc to run bash automatically add:

        if ($?prompt) then
                if ( -x /bin/bash ) exec /bin/bash
        endif

If you are editing .bashrc to make tcsh run, then add:

        if [ "$PS1" ]; then
                if [ -x /bin/tcsh ] ; then exec /bin/tcsh ; fi
        fi

The “if”s are being paranoid. The first one checks that you are running an interactive shell and aborts if you are not (to make sure you don’t break scp transfers and the like). The second “if” makes sure the shell exists and will execute, otherwise the exec statement will log you out before you get a chance to do anything.

The above is slightly inefficient as you have to execute two shells every time you log on. However, exec does replace the current shell, which means that the old shell doesn’t hog any memory. This method can also be useful to keep environment variables exported automatically by your original shell.