Env var expansion problem

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Env var expansion problem

Zdenek Sekera
Two problems:

- in vim do ':h expand<C-D>'
   it will list several tags, the 'expand-environment-var'
   does not exist (try ':h expand-environment-var')

- How can one get a value of an environmental variable
   when is it defined to be an array (I'm in 'ksh')

   Try this (in ksh):
   $ set -A A "a b c" "d e f"
   $ export A
   $ print ${A[0]}
   a b c
   $ print ${A[1]}
   d e f

   So far so good.

   Now startup vim and try:

   :echo $A
   a b c

   But how can I get the value of A[1]???
   The ':echo ${a[1]}' gives an error, obviously, any other
   way of writing I could try ends in error.
   Is it possible at all?

   I know the question is somewhat shell dependent, however all
   popular shells (zsh, ksh, bash) have some array syntax
   (all very similar) so if it cannot be done right now, perhaps
   it would be worth to add to vim.

   If I cannot do it directly would anybody imagine
   a trick/hack/idea/whatever to retrieve all values of
   an "environmental array" ? I really have a problem
   requiring this feature so I'd appreciate any help.

Thanks,

---Zdenek
Reply | Threaded
Open this post in threaded view
|

Re: Env var expansion problem

Matthew Winn
On Tue, Mar 21, 2006 at 02:57:18PM +0100, Zdenek SEKERA wrote:

> - How can one get a value of an environmental variable
>   when is it defined to be an array (I'm in 'ksh')
>
>   Try this (in ksh):
>   $ set -A A "a b c" "d e f"
>   $ export A
>   $ print ${A[0]}
>   a b c
>   $ print ${A[1]}
>   d e f
>
>   So far so good.
>
>   Now startup vim and try:
>
>   :echo $A
>   a b c
>
>   But how can I get the value of A[1]???

I doubt it's possible.  Values such as ${A[1]} are internal to the
shell; the value that the shell exports as $A is the value of ${A[0]}.

--
Matthew Winn ([hidden email])
Reply | Threaded
Open this post in threaded view
|

Re: Env var expansion problem

Nikolai Weibull-11
On 3/21/06, Matthew Winn <[hidden email]> wrote:
> On Tue, Mar 21, 2006 at 02:57:18PM +0100, Zdenek SEKERA wrote:
> >   But how can I get the value of A[1]???
>
> I doubt it's possible.  Values such as ${A[1]} are internal to the
> shell; the value that the shell exports as $A is the value of ${A[0]}.

Precisely.  The environment only contains mappings to strings.  In
Ksh, $A happens to expand to the contents of the array, so that's
what's put in the environment.  In Bash (and sh), $A expands to the
first element of the array, so that's what's put in the environment
there, I believe.

  nikolai
Reply | Threaded
Open this post in threaded view
|

Re: Env var expansion problem

Timo Hirvonen
On Tue, 21 Mar 2006 15:21:18 +0100
"Nikolai Weibull" <[hidden email]> wrote:

> Precisely.  The environment only contains mappings to strings.  In
> Ksh, $A happens to expand to the contents of the array, so that's
> what's put in the environment.  In Bash (and sh), $A expands to the
> first element of the array, so that's what's put in the environment
> there, I believe.

# echo $BASH_VERSION
3.1.5(1)-release
# a=(a b "foo bar") ./a.out a
a='(a b foo bar)'
# export a=(a b "foo bar")
# ./a.out a
a='(null)'     # 'a' is not in ./a.out's environment
# echo $a
a
# echo ${a[0]}
a
# echo ${a[1]}
b
# echo ${a[2]}
foo bar
# export | grep a=
declare -ax a='([0]="a" [1]="b" [2]="foo bar")

--
http://onion.dynserv.net/~timo/