Reading env POSIX documentation:
Some have suggested that env is redundant since the same effect is achieved by:
name=value ... utility [ argument ... ]
The example is equivalent to env when an environment variable is being added to the environment of the command, but not when the environment is being set to the given value. The env utility also writes out the current environment if invoked without arguments. There is sufficient functionality beyond what the example provides to justify inclusion of env.
AFAICT, the above statement meaning var=value command
will be the same as env var="value" command
, and not when using as env -i var="value" command
.
Now, at least with env
implementation on GNU system, FreeBSD and Solaris 11, I realize that they're not equivalent, because env
allow any characters, except =
and \0
in var
name:
$ env 'BASH_FUNC_foo%%=() { echo foo; }' bash -c foo
print foo, while you can't use BASH_FUNC_foo%%='() { echo foo; }'
in any shells, because BASH_FUNC_foo%%
clearly not a valid variable name.
In POSIX shells, except bash
, this left a variable named BASH_FUNC_foo%%
in environment variables, which the shell can not access it.
So, what is the purpose of allowing arbitrary name in form env var=value
and was it allowed by POSIX?
env
is a mistake.....especially including the python crowd's promotion of usingenv
on the#!
line of a script file. See http://unix.stackexchange.com/questions/29608/why-is-it-better-to-use-usr-bin-env-name-instead-of-path-to-name-as-my for interesting debate on the topic. – cas Oct 02 '15 at 05:19bash
. – cuonglm Oct 02 '15 at 06:28rc
that allows anything in a variable name (and where all variables are exported). – Stéphane Chazelas Oct 05 '15 at 19:35