Execute a command with another user’s environment

Assuming you have the correct privileges, sudo -u lets you execute a command as another user.

For example, the following command runs yarn install as the user jimbob:

sudo -u jimbob yarn install

There’s a catch though: the above command doesn’t inherit JimBob’s user environment. That is problematic if you depend on something in JimBob’s .bash_profile.

The -i flag fixes this problem.

sudo -iu jimbob yarn install

Sign up for my newsletter

A monthly round-up of blog posts, projects, and internet oddments.