[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Bash 3 breaks array expansion
From: |
William Park |
Subject: |
Re: Bash 3 breaks array expansion |
Date: |
12 Aug 2004 21:41:02 GMT |
User-agent: |
tin/1.6.2-20030910 ("Pabbay") (UNIX) (Linux/2.6.7 (i686)) |
Roy Marples <uberlord@rsm.demon.co.uk> wrote:
> Description:
> Bash 3 breaks array expansion
>
> Repeat-By:
> #!/bin/bash
> x=(one two)
> echo ${x[@]:1}
> # prints nothing in bash 3
> # prints two in bash 2
>
> x=(one two three)
> echo ${x[@]:1}
> # prints two three as expected in bash 2
>
> Fix:
> (really a work around)
> #!/bin/bash
> x=(one two)
> x=("${x[@]}" "xxx")
> echo ${x[@]:1:${#x[@]} - 2}
It is broken in verify_substring_values(). In Bash-3.0, the first
argument is array index, and the second argument is number of elements
(in storage, not in index). For your case, '1' is tested against
'max_index' which is 1, so it exits.
That is why, you get
x=(one two [9]=nine)
echo ${x[*]:1} --> 'two nine', as expected
echo ${x[*]:1:2} --> 'two nine', but 'two' was expected
echo ${x[*]:2:2} --> 'nine', but '' was expected
even though x[9] is outside of the index range specified.
Fix1:
case VT_ARRAYVAR:
a = (ARRAY *)value;
/* For arrays, the first value deals with array indices. */
- len = array_max_index (a); /* arrays index from 0 to n - 1 */
+ len = array_max_index (a) + 1; /* arrays index from 0 to n - 1 */
break;
Fix2:
Use positional parameter,
set one two
echo ${*:2}
--
William Park <opengeometry@yahoo.ca>
Open Geometry Consulting, Toronto, Canada