[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Clarification on Setting Environment Variables for Command Execution
From: |
Collin Funk |
Subject: |
Re: Clarification on Setting Environment Variables for Command Execution with or without env. |
Date: |
Wed, 05 Jun 2024 02:24:51 -0700 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
Hi,
Hongyi Zhao <hongyi.zhao@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