in reply to Set env variables as part of a command

not sure if that's what you want, but you don't need a subshell to have temporary environment variables (at least in bash)

lanx@lanx-1005HA:~$ echo $a,$b 0,0 lanx@lanx-1005HA:~$ a=666 b=42 perl -e 'print "@ENV{a,b}\n"' 666 42 lanx@lanx-1005HA:~$ echo $a,$b 0,0

please note the "missing" semicolon!

update

and the vars don't seem to show up in the process list, though I'm no expert there

lanx@lanx-1005HA:~$ ps -edaf |grep perl lanx 2731 2174 0 18:02 pts/2 00:00:00 perl -e print "@ENV{a, +b}\n";sleep 100

Anyway they'll be in the bash history.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^2: Set env variables as part of a command
by afoken (Chancellor) on Feb 18, 2017 at 18:30 UTC
    and the vars don't seem to show up in the process list, though I'm no expert there lanx@lanx-1005HA:~$ ps -edaf |grep perl

    ps' command line handling is quite messy: "e" and "-e" are completely different things. "-e" shows all processes, "e" shows the environment variables.

    Environment variables are always visible, temporary or not.

    So:

    >env - foo=bar environment=unsafe sleep 100 & [1] 28318 >ps e | grep sl[e]ep 28318 pts/0 S 0:00 sleep 100 foo=bar environment=unsafe >

    Or:

    >foo=bar environment=unsafe sleep 100 & [1] 28338 >ps e | grep sl[e]ep 28338 pts/0 S 0:00 sleep 100 foo=bar environment=unsafe TERM=x +term SHELL=/bin/bash (... and many more) >

    Update: So, effectively, there are no "temporary" environment variables, at least not when looking at process environments. Shells like bash have their own set of variables, they are often referenced to as environment variables, but they aren't, at least not all of them. "Exporting" variables, e.g. with export FOO, promotes a variable from a private piece of memory to an environment variable that is passed to other processes. Prefixing a command with variable=value does the same, inside the sub-process forked to execute the command.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)