3

How can I store an argument vector with an argument containing spaces in a bash variable?

For example, I want 2.sh to print b c

1.sh

#!/bin/bash

ARGV='a "b c"'
./2.sh $ARGV

2.sh

#!/bin/bash

echo $2
3

You should use an array instead of a string:

args=("a" "b c")
./2.sh "${args[@]}"

When the array expansion is quoted, each element of the array is properly expanded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.