-   Linux - Newbie (
-   -   Bash Variable Array, Trying to add another value into the array (

helptonewbie 12-22-2008 08:00 AM

Bash Variable Array, Trying to add another value into the array
Hi All,

Just thinking about a variable array and i'd like to if possible...
when working with an array simply add a value to the array at the next available slot/number so to speak.

i have an array:-

array=( `echo 1 2 3 4` )
this obviously giving me 4 values in my array

echo ${#array[@]}

Instead of then having to do something like:-

number=`echo ${#array[@]}`
((number++)) #add one

Is it possible to not have to know the next number in the array so the value can just be added to the next available slot in the array so to speak. ie


rather than going through the rigmarole of all that above. It would be much nicer if its possible to just say chuck this value anywhere in the array as i don't care where it goes. Just as long as its not overwriting something already in the array, and is using the next available number in the array so that when you do something like:-

echo ${#array[@]


echo ${array[@]
you still get returned the correct numbers of 5 and 1 2 3 4 5

Any ideas?


unSpawn 12-22-2008 04:53 PM

You could use 'array[$[${#array[@]}+1]]=Whatever'?

helptonewbie 12-22-2008 05:06 PM

Hey unspawn,
Thanks very much, looks perfect. I think i need to look into more details with bash and all the [] {} () (()) and how they all work.

Thanks though, i can see that should work fine.


kvaibhav 03-02-2009 07:33 AM

Dear all,
is it possible to add a constant integer to all array elements
in one go.


I have and array:


myarray=( 2 3 2 14 6 32 5 455 23 )

On completing the operation, I want the array to be modified as:


myarray=( 4 5 4 16 8 34 7 457 25 )
Just as one is able to substitute all strings in an array
in bash at one go, how do I do the above in one go?

Thanks! :)


helptonewbie 03-02-2009 07:50 AM

Hello There,
Not sure if this is the best way but a quick go at it from my point of view and i come up with:-


# myarray=( 4 5 4 16 8 34 7 457 25 )
# count=0
# for i in ${myarray[@]}; do myarray[$count]=`expr $i + 2`; ((count++));done
# echo ${myarray[@]}
6 7 6 18 10 36 9 459 27

Just change the "2" in the `expr $i + 2` to be what ever amount you want to be added to each number in the variable.

Hope this helps,

theYinYeti 03-02-2009 07:55 AM

Array indexes are zero-based. So you don't need to compute anything. Example:

[yves@localhost ~]$ array=( 1 2 3 4 )
[yves@localhost ~]$ echo ${#array[*]}
[yves@localhost ~]$ array[${#array[*]}]=5
[yves@localhost ~]$ array[${#array[*]}]=6
[yves@localhost ~]$ for ((i=0; i<${#array[*]}; i++)); do echo "array[$i] = ${array[$i]}"; done
array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5
array[5] = 6



[yves@localhost ~]$ for ((i=0; i<${#array[*]}; i++)); do array2[$i]=$((${array[$i]} + 2)); done

kvaibhav 03-02-2009 11:18 PM

Dear all,
thanks for the reply.

The solution IS evident and thanks for it.

What I was looking for, was an 'array operation' that does the same.

For example, in the following:


# Replace all occurrences of substring.
echo ${arrayZ[@]//iv/YY}    # one two three four fYYe fYYe
                            # Applied to all elements of the array.


all elements of array get altered on just one array operation.

I was looking for such a solution.




All times are GMT -5. The time now is 11:07 PM.