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.
VAR=asdf
does update the environment ifVAR
was already in the environment. (This wasn't true in the original Bourne shell.) – Gilles 'SO- stop being evil' Jun 03 '17 at 22:32