help-bash
[Top][All Lists]
Advanced

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

Re: access exported var


From: Peng Yu
Subject: Re: access exported var
Date: Sun, 15 Jan 2023 23:37:41 -0600

On 1/15/23, Lawrence Velázquez <vq@larryv.me> wrote:
> On Mon, Jan 16, 2023, at 12:10 AM, Peng Yu wrote:
>> The results are different depending on whether the variable is a
>> scalar or an array (the result is always null). Why is it so?
>
> Because arrays cannot be exported to the environment.

If that is the case, why bash does not print an error message?

>> Why even in the last case, "a" of the first element of the array
>> cannot be retrieved?
>
> Because the array is not in the environment.
>
> This slightly different version of your test might make this clear
> (you could also use env(1) or whatever other tool you like):
>
>       $ cat foo.c
>       #include <stdlib.h>
>       #include <stdio.h>
>
>       int main(int argc, char *argv[]) {
>           const char *s = getenv(argv[1]);
>           if (s) {
>               printf("<%s>\n", s);
>           } else {
>               puts("not found");
>           }
>           return 0;
>       }
>
>       $ cc foo.c
>       $ export W=''
>       $ ./a.out W
>       <>
>       $ export X=abc
>       $ ./a.out X
>       <abc>
>       $ declare -A Y=([a]=1 [b]=2)
>       $ export Y
>       $ ./a.out Y
>       not found
>       $ Z=(a b)
>       $ export Z
>       $ ./a.out Z
>       not found
>
> --
> vq
>


-- 
Regards,
Peng



reply via email to

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