nmh-workers
[Top] [All Lists]

Re: [Nmh-workers] Environment variables - with or without $?

2017-03-24 06:36:02
Hi Valdis,

No dollar.  I have an environment variable called `$FOO'.

Which shows up that way in 'printenv', and requires the use of $$FOO
to dereference? (Some quick testing with bash 4.4 seems to indicate
that it *really* doesn't want to see $ as the first character of a
variable name.  I had to use 'env' to jam it in there, and accessing
it was.. problematic

Creating it, and seeing it's there, is straightforward, as you found.

    $ env \$FOO=42 env | grep '^\$FOO='
    $FOO=42
    $
    $ f=/proc/self/environ
    $ env \$FOO=42 grep -z '^\$FOO=' $f | tr \\0 \\n
    $FOO=42
    $

And bash is passing it on to its sprogs.

    $ env \$FOO=42 bash -c 'grep -z '\''^\$FOO='\'' '$f | tr \\0 \\n
    $FOO=42
    $

But bash 4.4.012-2 here filters $FOO out before setting its shell
variables.

    $ env -i \$FOO=42 bash -c set | sed 's/=.*//' | sort | fmt
    _ BASH BASH_ALIASES BASH_ARGC BASH_ARGV BASH_CMDS BASH_EXECUTION_STRING
    BASH_LINENO BASHOPTS BASH_SOURCE BASH_VERSINFO BASH_VERSION DIRSTACK
    EUID GROUPS HOSTNAME HOSTTYPE IFS MACHTYPE OPTERR OPTIND OSTYPE PATH
    PPID PS4 PWD SHELL SHELLOPTS SHLVL TERM UID
    $

This means it isn't there to access indirectly.  Indeed, bash seems to
dislike the attempt.

    $ env BAR=314 bash -c 'n=BAR; echo $n=${!n}'
    BAR=314
    $ env \$FOO=42 bash -c 'n=\$FOO; echo $n=${!n}'
    bash: $FOO: bad substitution
    $

But that's bash.  Other interpreters give access.

    $ env \$FOO=42 perl -le 'print $ENV{'\''$FOO'\''}'
    42
    $ env \$FOO=42 python -c 'import os; print(os.environ["$FOO"])'
    42
    $

dash(1), unlike bash, filters it out from nippers as well as its own
variables.

    $ env -i \$FOO=42 dash -c set | sed 's/=.*//' | sort | fmt
    ' IFS LINENO OPTIND PATH PPID PS1 PS2 PS4 PWD
    $
    $ env \$FOO=42 dash -c 'grep -z '\''^\$FOO='\'' '$f | tr \\0 \\n
    $

(That «'» is the closing quote of IFS on a line of its own.)

Ditto Heirloom sh, a way of getting close to 7th Ed sh.

    $ env -i \$FOO=42 /usr/heirloom/bin/sh -c set | sed 's/=.*//' | sort | fmt

    IFS MAILCHECK OPTIND TIMEOUT
    $
    $ env \$FOO=42 /usr/heirloom/bin/sh -c 'grep -z '\''^\$FOO='\'' '$f | tr 
\\0 \\n
    $

(The blank line is IFS again.)

bash before https://en.wikipedia.org/wiki/Shellshock_(software_bug) may
have behaved differently;  I don't have one to hand.  They tightened up
lots in this area then.

-- 
Cheers, Ralph.
https://plus.google.com/+RalphCorderoy

_______________________________________________
Nmh-workers mailing list
Nmh-workers(_at_)nongnu(_dot_)org
https://lists.nongnu.org/mailman/listinfo/nmh-workers

<Prev in Thread] Current Thread [Next in Thread>