expand arguments from array containing double quotes

Viewed 1581

I want to call a program with arguments built from an array in bash.

I want bash to call:

echo -arg1=simple -arg2="some spaces"

from array=(echo -arg1=simple "-arg2=\"some spaces\"") or similar (I can adjust the way the items are created).

Problem

With "${array[@]}" bash calls:

echo -arg1=simple '-arg2="some spaces"'

But I do not want the single quotes. How to build and expand the array correctly?

Example code

#!/bin/bash
set -x

array=()
array+=(echo)
array+=(-arg1=simple)
array+=("-arg2=\"some spaces\"")

"${array[@]}"
"${array[*]}"
${array[@]}
${array[*]}

Resulting calls

echo -arg1=simple '-arg2="some spaces"'
'echo -arg1=simple -arg2="some spaces"'
echo -arg1=simple '-arg2="some' 'spaces"'
echo -arg1=simple '-arg2="some' 'spaces"'
2 Answers
Related