[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: passing array to command line argument.
From: |
Stephane Chazelas |
Subject: |
Re: passing array to command line argument. |
Date: |
Tue, 9 Dec 2008 14:47:52 +0000 |
User-agent: |
Mutt/1.5.16 (2007-09-19) |
On Tue, Dec 09, 2008 at 09:14:51AM -0500, Chet Ramey wrote:
> >
> > Hello i would like to pass an array to my script command line argument, but
> > only the first element of the array is displayed. Here is my process :
> >
> > script1:
> > my_array=(el1 el2 el3)
> > script2 -f $my_array
>
> You're only passing the first element of the array to script2. An
> unsubscripted word expansion expands to the first element of an array.
[...]
More exactly, an unsubscripted word expansion expands to the
element of subscript 0 or to the empty string if that element is
not defined.
After
a[12]=foo
The first element is "foo", but $a expands to the empty string.
$a is a shortcut for ${a[0]} and a=bar is a shortcut for a[0]=bar
This is similar to ksh but different from zsh where arrays and
scalars are of different types, and arrays are not scarse arrays
but normal arrays. In zsh, a[12]=foo allocates an array of 12
elements, the first 11 being empty; $a is the same as $a[*] and
is the list of non-empty elements in $a. Doing a=foo, would
change the type of $a to be a scalar, so you'd lose all the
array elements. The OP's code is actually zsh (or rc/es) syntax,
though it would make more sense to do:
scalar -f "${my_array[@]}"
which would work the same in bash, ksh93 and zsh (and in zsh, it
wouldn't discard the empty elements, contrary to $my_array).
--
Stéphane