Skip to content

Instantly share code, notes, and snippets.

@organman91
Last active October 3, 2016 12:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save organman91/9204609 to your computer and use it in GitHub Desktop.
Save organman91/9204609 to your computer and use it in GitHub Desktop.
Boostrapping zsh from bash
If you're in an environment where your login shell isn't changeable (perhaps an educational institution)
but you still have the option to use zsh, you might want to have it log in to zsh automatically.
Some clever .bashrc will take care of that in a jiffy.
Put the following at the bottom of your .bashrc:
-----
# Start ssh-agent
# If (like me) you've got an NFS-mounted home directory,
# you might only want to start ssh-agent from the first machine you log in to.
# $$ is the PID of the process (in case you do terminal multiplexing and such)
if [ ! -e ~/.ssh/lockfile ]; then
echo $(hostname).$$ > ~/.ssh/lockfile
eval $(ssh-agent)
ssh-add
fi
# Check if zsh exists and, if so, exec it
# Won't start if this isn't a login shell (if you need to enter bash from within zsh)
# exec replaces the current process with a new one, which eliminates the need to
# exit twice (exit zsh and exit bash)
if [ -n "`which zsh`" -a $0 == "-bash" ]; then
exec -l zsh
fi
# Since exec replaces the bash process, nothing below this will run!
-----
If you're using ssh-agent, you'll need a .zlogout and a .bash_logout
At the top of .zlogout:
-----
# Cleanup ssh-agent
if [[ -e ~/.ssh/lockfile && "$(cat ~/.ssh/lockfile)" == "$(hostname).$$" ]]; then
rm ~/.ssh/lockfile
ssh-add -D
eval $(ssh-agent -k)
fi
-----
At the top of .bash_logout:
-----
# Cleanup ssh-agent (if zsh didn't)
if [ -e ~/.ssh/lockfile -a "$(cat ~/.ssh/lockfile)" == "$(hostname).$$" ]; then
rm ~/.ssh/lockfile
ssh-add -D
eval $(ssh-agent -k)
fi
-----
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment