I am writing a bash script that generates a list of arguments to pass to another, external script. This list of arguments can contain strings with spaces in it. Note that this list of arguments will be of varying length depending on how the script is run.
The bash script looks like this:
#!/bin/bash
# script1.sh
... some code that populates the array ARGS_TO_PASS ...
# NOTE! ARGS_TO_PASS can be of varying length depending on what the above code does.
#
# Here is example of what ARGS_TO_PASS might be:
# ARGS_TO_PASS=(a b c 'the quick brown fox')
# Now, call script2.sh on ARGS_TO_PASS.
script2.sh ${ARGS_TO_PASS[*]}
Here is the second script:
#!/bin/bash
# script2.sh
while [[ $# -gt 0 ]]; do
echo "parsing arg: $1"
shift
done
If the last line of script1.sh
is script2.sh ${ARGS_TO_PASS[*]}
the output looks like this:
parsing arg: a
parsing arg: b
parsing arg: c
parsing arg: the
parsing arg: quick
parsing arg: brown
parsing arg: fox
If the last line of script1.sh
is script2.sh "${ARGS_TO_PASS[*]}"
the output looks like this:
parsing arg: a b c 'the quick brown fox'
Neither of those are what I want. What I want is the output to look like this:
parsing arg: a
parsing arg: b
parsing arg: c
parsing arg: the quick brown fox
Is there any way to do this?