0

What's really the difference between:

env VARIABLE=VALUE command

and

VARIABLE=VALUE command

in bash-compatible shells?

Note: I've done some testing myself and I couldn't see any difference

Edit: An example would be

EDITOR=nano visudo

vs

env EDITOR=nano visudo
Kusalananda
  • 333,661
Gray K
  • 3

1 Answers1

1

Not much difference in the result for external commands. Running env just involves an additional exec to do the variable assignment parsing which the shell would otherwise do. env -i would be more useful, since it clears the environment, which isn't that straightforward to do in the shell.

In both cases, expansions like command substitutions VAR=$(somecmd) run by the shell before the assignments happen, and both cases follow PATH etc. The only difference I can come up with is with the value of the _, which Bash sets to the name of the command it runs (but env doesn't). Of course, without env, you might also run the shell's builtin version of the command.

ilkkachu
  • 138,973