Hi,

Hongyi Zhao <hongyi.z...@gmail.com> writes:

> Using env command:
>
> env PATH="/custom/path:$PATH" my_command
>
> Directly setting the environment variable:
>
> PATH="/custom/path:$PATH" my_command
[...]
> In what scenarios would it be more appropriate to use env versus
> directly setting the environment variable? Are there specific
> advantages or disadvantages associated with each method?

One minor problem is that csh [1] doesn't use the same syntax for
variable assignment. Up until recently FreeBSD used csh as the default
login shell for root accounts. I doubt you will run into the problem
anywhere else.

Here is an example:

    $ bash
    $ env | grep '^VAR'
    $ env VAR='abc' | grep '^VAR'
    VAR=abc
    $ VAR='abc' env | grep '^VAR'
    VAR=abc
    $ csh
    % env | grep '^VAR'
    % env VAR='abc' | grep '^VAR'
    VAR=abc
    % VAR='abc' env | grep '^VAR'
    VAR=abc: Command not found.

You could use:

    % setenv VAR 'abc' ; env | grep '^VAR'

But that would set the environment variable in the parent process which
isn't the intention.

Collin

[1] https://en.wikipedia.org/wiki/C_shell

Reply via email to