3

I have a script the manipulates data, creates arguments and sends them to a second script. One of the arguments contains a space.

script1.sh:

args=()
args+=("A")
args+=("1 2")
args+=("B")
. script2.sh ${args[@]}

script2.sh:

for f in "$@"
do
  echo f=$f
done

I'd like to get "1 2" as single argument, but receive them separately:

f=A
f=1
f=2
f=B

I tried also converting the input in script2 to list in=($@) and iterating over it using for f in ${in[@]} but got the same result.

So, the problem might be in one of the following: building the list of arguments ; passing the built list ; parsing the input ; or iterating over the input.

How to pass an argument with space from bash script to bash script? Thanks.

* I'm using git-bash on Windows.

AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277

1 Answers1

7

You just need to add quotes when passing the array as an argument:

args=()
args+=("A")
args+=("1 2")
args+=("B")
. script2.sh "${args[@]}"
arco444
  • 22,002
  • 12
  • 63
  • 67