PeterH
PeterH

Reputation: 171

Bash: Call script with an array of arguments

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'.

Upvotes: 0

Views: 263

Answers (1)

RTLinuxSW
RTLinuxSW

Reputation: 842

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[@]}"

Upvotes: 1

Related Questions