views:

232

answers:

1

I have a strange issue with array manipulation within a bash script on Solaris. I am using the syntax ${varName[@]:index} to obtain all of the elements in array varname after the specified index. However, if there is only one element after the specified index, nothing is returned.

This can be easily demonstrated by example:

#!/bin/bash

paramArray=( a b c )
echo "everything after 2" ${paramArray[@]:2} # Should display c but doesn't
echo "parameter 2 only  " ${paramArray[2]}   # Correctly displays c

paramArray=( a b c d e )
echo "everything after 2" ${paramArray[@]:2} # Correctly displays c d e
echo "parameter 2 only  " ${paramArray[2]}   # Correctly displays c

This code works correctly on a Windows box running Cygwin, but fails on Solaris (version: Solaris 9 9/05 s9s_u8wos_05 SPARC)

Can anyone explain this behaviour?

+2  A: 

That sounds like a bug in the version of Bash on your Solaris system.

What versions (bash --version) are you running both your systems?

You might look through the CHANGELOG and search for array bugs that are fixed after the release of your Solaris version and before the release of your Cygwin version.

Chris Johnsen
Candidate: 3.0-release to 3.1-alpha - "o. Fixed off-by-one error when calculating the upper bound of `offset' when processing the ${array[@]:offset:length} expansion."
Dennis Williamson
Bash version is : GNU bash, version 3.00.0(1)-release (sparc-sun-solaris2.9)So yes, it's a bug on my system! Thanks Chris and Dennis for helping me out on this.
Phil