0

I'm trying to create an array within a bash script. I am doing this:

#!/bin/bash
declare -a testArray1=('a/b/c.def -x -y -z','x/y/z.000 -a -b -c')

echo "testArray1[0] = ${testArray1[0]}"
echo "testArray1[1] = ${testArray1[1]}"

But it doesn't work. Everything is being added to a single [0] element for the array.

What am I doing wrong?

  • 1
    Use space instead of , as element separator. – jimmij Jan 8 '16 at 10:48
  • simple when you know how. . . Cheers, – IGGt Jan 8 '16 at 11:01
2

Use this instead:

declare -a testArray1=('a/b/c.def -x -y -z' 'x/y/z.000 -a -b -c')

BTW: you can check the array structure with declare -p, see:

$ declare -a testArray1=('a/b/c.def -x -y -z','x/y/z.000 -a -b -c')
$ declare -p testArray1
declare -a testArray1='([0]="a/b/c.def -x -y -z,x/y/z.000 -a -b -c")'
$
$ declare -a testArray1=('a/b/c.def -x -y -z' 'x/y/z.000 -a -b -c')
$ declare -p testArray1
declare -a testArray1='([0]="a/b/c.def -x -y -z" [1]="x/y/z.000 -a -b -c")'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.