Running multiple commands in terminal

terminal

I'm told to run this command on terminal:

sudo env PATH=$PATH:/usr/local/bin /usr/local/lib/node_modules/pm2/bin/pm2 unstartup launchd -u dora --hp /Users/dora

It seems like it's multiple commands in one line:

sudo env PATH=$PATH:/usr/local/bin

/usr/local/lib/node_modules/pm2/bin/pm2 unstartup launchd -u dora --hp /Users/dora

But, how does that work? I know A;B, A&&B, A||B, but this looks like A B.

Best Answer

It indeed are multiple commands which get run here. If you look up man env you'll find

SYNOPSIS
    env [-iv] [-P altpath] [-S string] [-u name] [name=value ...] [utility [argument ...]]

which explains that env does, among other things,

  • assign values to variables (the [name=value ...] bit which in your case is PATH=$PATH:/usr/local/bin /usr/local/lib/node_modules/pm2/bin/pm2)
  • and then runs another program passed as an argument (the [utility [argument ...]] bit, in your case the call to npm).

The intention here is to extend the search PATH of the command run through sudo in order to have npm find all the executables it needs.

This is purely a feature of env, other commands like sudo or xargs have similar capabilities. OTOH A;B, A && B or A || B are shell features.