help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Remove an empty (null) element in an indexed array


From: John Kearney
Subject: Re: [Help-bash] Remove an empty (null) element in an indexed array
Date: Thu, 25 Apr 2013 22:22:33 +0200

I also can't reproduce your problem.

generally if you want to have a contiguous array the easiest way is normally

declare -a FOO=(red blue green white black)
unset 'FOO[2]'
FOO=( "address@hidden" )


but if you just want to iterate over the sparse array you can just do.

for ckey in address@hidden; do
   echo "${FOO[${ckey}]}"
done







On Thu, Apr 25, 2013 at 2:52 PM, Greg Wooledge <address@hidden> wrote:
On Wed, Apr 24, 2013 at 05:28:23PM -0400, Jerry wrote:
> Assume array:  declare -a FOO=(red blue green white black)
>
> Now, address@hidden would return 5
>
> If I then do: unset FOO[2}, the "green" element is erased and
> address@hidden returns 4. However, if I do:

You should use

  unset 'FOO[2]'

with the quotes.  If you omit the quotes, and if there just happens to
be a file named FOO2 in the current directory, then Bash will perform
globbing and convert the command into

  unset FOO2

which will not do what you expected.

> echo address@hidden

Again, the quotes are very important.

  echo "address@hidden"

Or better still,

  printf '<%s> ' "address@hidden"; echo

I cannot duplicate your results any more than Chet could.  Even in a very
old version of Bash:

imadev:~$ bash-2.05b
imadev:~$ FOO=(red orange yellow green blue indigo violet)
imadev:~$ unset 'FOO[2]'
imadev:~$ printf '<%s> ' "address@hidden"; echo
<red> <orange> <green> <blue> <indigo> <violet>

Perhaps you've got IFS set to something weird, and that together with
your missing quotes is causing unexpected oddness.  But I can't guess
what value of IFS would cause it.



reply via email to

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