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.

I saw the following snippet in this thread: How to change from csh to bash as default shell

sleep 2
if (-x /usr/local/bin/bash) then
  exec /bin/sh -c '. ~/.profile; exec /usr/local/bin/bash'
endif

My understanding is that if you place this snippet in ~/.login it will invoke bash when you login.

I have a similar situation where I would like to use a similar snippet to invoke a version of zsh that is different from the default one with which I log in. Part of the reason why I am going through this trouble is because I can't choose my desired version of zsh in the options allowed in chsh, and I don't have administrator privileges.

With this:

  • How would I change the snippet above to do this when switching between different versions of zsh?
  • Why are two exec commands needed in the snippet above? What do they do? Also, why does the snippet above use sh and bash (the user is supposed to login with csh)
share|improve this question

1 Answer 1

up vote 4 down vote accepted

If you want to execute zsh instead of bash, just replace /usr/local/bin/bash by the path to the zsh executable. Note that this snippet is specific to csh; if your login shell is not (t)csh, this code in ~/.login won't do anything for you.

As for why the snippet was written that way, you'd have to ask the person who wrote it. [Checks who that was.] Oh. Well, if you just do exec /usr/local/bin/bash then you won't be starting a login shell, so your .profile won't be executed. But this could be resolved in a simpler way by executing /usr/local/bin/bash --login.

Now, if your login shell is not csh, you'll want a different snippet around exec. In Bourne-style shells, including zsh:

if [ -x ~/bin/zsh ]; then exec ~/bin/zsh; fi

Make that exec ~/bin/zsh -l if you want the new instance of zsh to read your ~/.zprofile. Note that if you do that, the snippet above must be in your .profile or .bash_profile; if your login shell is zsh, don't put the snippet in your .zprofile, or else make very sure that the new instance of zsh isn't going to call that exec again.

share|improve this answer
    
Thanks @Gilles. I wrote the following on my .zprofile: if [[ $ZSH_VERSION != 4.3.14 ]]; then exec /n/sw/zsh-4.3.14/bin/zsh --login fi and that seems to work! –  user815423426 Dec 11 '11 at 22:53

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.