Bash – the difference between env, setenv, export and when to use

bashenvironment-variablesshell

Recently I noticed we have 3 options to set environment variables:

  1. export envVar1=1
  2. setenv envVar2=2
  3. env envVAr3=3

If there are other ways, please enlighten us.

When should I prefer one over the other? Please suggest guidelines.

As for shell compatibility, which is the most expansive (covers more shell dialects)?

I already noticed this answer but I wish to expand the question with env and usage preference guidelines.

Best Answer

export VARIABLE_NAME='some value' is the way to set an environment variable in any POSIX-compliant shell (sh, dash, bash, ksh, etc.; also zsh). If the variable already has a value, you can use export VARIABLE_NAME to make it an environment variable without changing its value.

Pre-POSIX Bourne shells did not support this, which is why you'll see scripts that avoid export VARIABLE_NAME='some value' and use VARIABLE_NAME='some value'; export VARIABLE_NAME instead. But pre-POSIX Bourne shells are extremely rare nowadays.

setenv VARIABLE_NAME='some value' is the csh syntax to set an environment variable. setenv does not exist in sh, and csh is extremely rarely used in scripts and has been surpassed by bash for interactive use for the last 20 years (and zsh for even longer), so you can forget about it unless you encounter it.

The env command is very rarely useful except in shebang lines. When invoked without arguments, it displays the environment, but export does it better (sorted, and often quoted to disambiguate newlines in values from newlines that separate values). When invoked with arguments, it runs a command with extra environment variables, but the same command without env also works (VAR=value mycommand runs mycommand with VAR set to value, just like env VAR=value mycommand). The reason env is useful in shebang line is that it performs PATH lookup, and it happens to not do anything else when invoked with a command name. The env command can be useful to run a command with only a few environment variables with -i, or without parameters to display the environment including variables with invalid names that the shell doesn't import.

Related Question