Flip from Work to Personal and Back Quickly with a Shell Script

I maintain separate personal and work accounts for as much as I possibly can. Email and calendar are the obvious ones. But GitHub, NPM, and remote access to other servers via SSH, I keep separate too. Sometimes this is difficult, though, because many tools just aren’t made to support this. I’ve tried heavyweight solutions over the years like multi-boot to separate user accounts on my Mac. They all have some infuriating flaw, so why not try a simple shell script?

Enter use

What I’ve settled on is use, a tiny script that will set up parts of my local environment by swapping in configurations. It looks like this:


if [ ! -n "$use" ]; then use=personal; fi

ssh-add -D
ssh-add --apple-use-keychain "$HOME/.ssh/${use}_ed25519"

	if [ -L "$1" ]; then rm "$1"; fi
	ln -s "$1-${use}" "$1"

link "$HOME/.gitconfig"
link "$HOME/.npmrc"

I put this script in .local/bin in my home directory, adding the same directory to my PATH. Now I’m able to type, for example—

use atomic

— and the following will happen:

The keys in my SSH agent are cleared out, and my Ed25519 key called atomic_ed25519 key is loaded. SSH is actually pretty smart about having lots of keys available and only authenticating with the correct one, but Git over SSH is not.

The reason for this is because SSH comes first in the connection. If you’re using a service like GitHub, it’s likely all your SSH keys are valid—but once the SSH connection is established, you may have the wrong key for the access you specifically want.

So I get this to work by making sure only the keys I need for the work I’m presently doing are loaded.

The global Git config in .gitconfig-atomic is linked to .gitconfig. My user email config lives here, so my commits will be correctly signed with my work email address when I’m using this configuration.

My NPM credentials in .npmrc-atomic is linked to .npmrc. I have two NPM accounts—one personal, one for work. Neither can publish to the other’s packages or scopes. Picking the right one is important.

Future expansions to the shell script?

These three items are all use does for me right now, but I could easily add things as I find them. In fact, I just added NPM today, which is why I decided to write this post!

There never will be a perfect solution for this, but this quick tool has helped keep some of the most important things separate.