0

I have a var which is a string that contains several words like this:

my_var="foo bar baz"

I'd like to call another script and pass in my_var. I want this second script to see three arguments foo bar baz instead of a single argument "foo bar baz"

How is this done? Here's what currently have:

text_output=$( /bin/bash /the/second/script.sh ${my_var} )

This second script is expecting any number of arguments. And currently it says it is getting one argument. Could the syntax ${} be the issue?

2
  • 3
    If you aren't quoting ${my_var}, something in your script is miscounting the number of arguments it is receiving. Commented May 1, 2015 at 18:43
  • Either that, or IFS got changed and that's confusing things. Commented May 2, 2015 at 5:30

2 Answers 2

2

Don't use a plain parameter to store multiple arguments; this is what arrays are for.

declare -a my_arr    # optional
my_arr=( foo bar baz )
text_output=$( /bin/bash /the/second/script.sh "${my_arr[@]}" )
Sign up to request clarification or add additional context in comments.

Comments

0

You can pass it unquoted so that shell will be able to expand it. Examples:

my_var="foo bar baz"

fn() { echo $#; }

fn "$my_var"
1

fn $my_var
3

Comments

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.