coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: env: can it be used to let only certain variables through (if they'r


From: Pádraig Brady
Subject: Re: env: can it be used to let only certain variables through (if they're set)?
Date: Fri, 21 Jan 2022 14:33:43 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:95.0) Gecko/20100101 Thunderbird/95.0

On 21/01/2022 00:28, Christoph Anton Mitterer wrote:
Hey.

What I'd like to do, is set up a shebang line e.g. for shell scripts,
so that the environment (in terms of variables) it gets is kinda
predictable.

For certain tasks this is very easy, e.g. I can set some
PATH=/usr/bin:/bin or so.

It's also easy to filter out anything I don't set in specific, by using
-i ... so I can get rid of things like POSIXLY_CORRECT which would
alter behaviour of tools like GNU's sed & friends... and also
everything in terms of such variable which I don't even think about.



Now what I cannot get fully working, is to let certain variables
through, only if they are set.
E.g. it could make sense to block LC_CTYPE, but let LC_MESSAGES
through.

/usr/bin/env -iS 'LC_MESSAGES=${LC_MESSAGES} command args'

get's close to it, but has the problem that LC_MESSAGES is set (though
empty) if it wasn't set outside.

And there are env vars, which have altering behaviour to certain
programs, purely when they're set (regardless of whether empty or so).



On the command line (i.e. without shebang) I can do:

/usr/bin/env -i ${LC_MESSAGES+"LC_MESSAGES=${LC_MESSAGES}"} command args

can this be done for -S, too? Or added as a feature?

--split-string doesn't support that currently.
The standard shell syntax to support
setting a variable iff it's not previously set is:
  ${var="value"}
Or relaxing a bit to set a variable if it's empty or unset use
  ${var:="value"}

So expanding your example this would be:
  ${LC_MESSAGES=${LC_MESSAGES}}

Arguments against this would be:
  Quite obtuse syntax
  Wouldn't be backwards compat with older or other env -S implementations
  Quite verbose as you would probably want to list many of these on the -S line

Another possibility to implement this functionality
while avoiding all of the above disadvantages
would be to augment the recently proposed --file option
to also support removing, or passing if set functionality, like:

  var1=explicit_val
  var2=${referenced_val}
  # explicitly remove var3
  -var3
  # pass var4 through if set (for use with -i)
  +var4
  # pass var5 if set, or else init to val
  +var5=val
  # pass var6 if non empty, or else init to val
  :var6=val
  # pass var7 if non empty
  :var7

Another possibility might be to support a POSIX shell compat syntax for this?

  var1=explicit_val
  var2=$'escaped$\nval'
  unset var3
  : ${var4=${var4}}
  : ${var5=val}
  : ${var6:=val}
  : ${var7:=${var7}}

I'm leaning towards the non shell compat syntax,
but need to think a bit more about this.

cheers,
Pádraig



reply via email to

[Prev in Thread] Current Thread [Next in Thread]