[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Clarification on Setting Environment Variables for Command Execution
From: |
Hongyi Zhao |
Subject: |
Re: Clarification on Setting Environment Variables for Command Execution with or without env. |
Date: |
Thu, 6 Jun 2024 14:13:58 +0800 |
On Wed, Jun 5, 2024 at 5:24 PM Collin Funk <collin.funk1@gmail.com> wrote:
>
> 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
Thank you very much to everyone here for their responses to this
issue. I need to carefully read the relevant explanations and further
observe in subsequent practices to see if I have fully understood the
issue or if there are still areas that need further discussion.
Regards,
Zhao