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.

2
  • A combination of "${args[@]}" with for f in "$@" did the trick. Thanks. Commented Jul 30, 2015 at 14:55
  • Just for the record for f; do is the same as for f in "$@"; do. Commented Jul 30, 2015 at 15:15

1 Answer 1

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[@]}"
Sign up to request clarification or add additional context in comments.

3 Comments

@AlikElzin-kilaka: On the receiving side, use quotes, too: in=("$@")
A combination of "${args[@]}" with for f in "$@" did the trick. Thanks.
@AlikElzin-kilaka I was going to suggest the for f in "$@" but in your example it was already quoted so I was puzzled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.