0

Given a bash array, I'd like to call another script with that array as an argument. I expected that the ${X[@]} would do what I want, but that string concats the args, rather than passing args separately. For example:

X=()
X+='a'
X+='b'
./p.sh ${X[@]}

Calls p.sh with 1 arg whose value is 'ab'. I want to call p.sh with 2 args, the first being 'a' the second being 'b'.

1
  • 1
    You add array elements with X+=('a') Commented May 16, 2017 at 20:58

1 Answer 1

1

A echo ${X[0]} outputs ab which shows your += appends to the first element. (The command echo ${#X[@]} outputs a 1 as well.) Use

X=()
X+=('a')
X+=('b')
./p.sh "${X[@]}"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @chepner for the double quotes. It is a more correct answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.