bash, command line substitution

I have one script calling another with a set of strings that includes white space. Script A calls Script B with these input strings: one two "th ree"

Script B pulls apart the arguments correctly:
arg0 = one, arg1 = two, arg2 = "th ree"

if I call it from within Script A like so:

../scriptB.sh  one two "th ree"

However I'd like to have the list of arguments contained within a variable. e.g. my_args = one two "th ree" and then call it like:

../scriptB.sh  $my_args

But whenever I try to put that list in a variable the args wind up as:
arg0 = one, arg1 = two, arg2 = "th, arg3 = ree"

Tried many combinations. Is there some kind of limitation regarding "$@"?

Here is script B (shamelessly plagiarized from the net):

#!/bin/bash 
echo "------------------------------"
echo allargs: "$@"

# build array from input $@ to ensure multi-word 
# elements can include whitespace

i=0
for arg in "$@"
do
	argArray[$i]=$arg
	let "i+=1"
done

elem_cnt=${#argArray[@]}
echo elem count: $elem_cnt

indx=0

# print array contents
while [ "$indx" -lt "$elem_cnt" ]
do
	echo "arg[$indx]: ${argArray[indx]}"
	let indx+=1
done

exit 0

Now I'm sure there are many ways to successfully script the functionality I described, but I'm mainly interested in understanding this problem. Is what I'm attempting impossible? And if so why?.

You could try

$ my_args="one,two,th ree"
$ OIFS=$IFS
$ IFS=,
$ ../scriptB.sh  $my_args
$ IFS=$OIFS

The magic combination is "$@". It's special because most things in quotes don't split at all, but "$@" does split -- but only on arguments, never on IFS.

You can feed this into a BASH array easily.

ARR=( "$@" )

This will put all the arguments into an array one at a time without splitting on anything at all.

If this doesn't work, it's not your script that's splitting them, but something earlier.

Once again I've somehow failed to clarify the gist of my question. Apologies, and thanks for responding.
Script B, which splits the arguments as desired, is not the problem. It uses "$@" but just loops over the args to give us a verification.
It's formatting of the input that has my stymied.
So let's assume we don't alter Script B. And Script A receives input that it has to pass onto Script B.
If we echo the input from within Script A it looks like:
one two "th ree"
If we take the above line and paste it "as is" into the call to Script B:

/home/scriptB.sh  one  two  "th ree"

then the args get separated as desired.
But if we try to configure a variable with the same contents as the above string AND we want the same result:

/home/scriptB.sh $mystr

then it does not work for me.
I don't know how to format the string of the variable such that it is processed identically to the method of just manually entering the chars.

Ah. I see now.

The only way to perfectly do what you want is eval, which isn't just awkward, it's dangerous:

# eval considers a string as its own shell statement, which 
# will process one two "th ree" as three strings as intended.
$ STR='one two "th ree"'
$ eval "printf \"%s\n\" $STR"
one
two
th ree

# But this means it will swallow ANY valid shell syntax, including
# backticks, variable assignments, etc, etc, etc.
# Someone could make your program do anything by feeding
# it the right string.
$ STR='one two "th ree" `touch /`'
$ eval "printf \"%s\n\" $STR"
touch: setting times of `/': Permission denied
one
two
th ree

Why not just use an array in the first place? Much simpler, much safer.

:b: Much thanks, Corona. Eval did the trick.
I can reformat the input before passing it to script B. But once the input is in a variable and passing the variable to script B - the elements get split on the space within the multi-word element. And it keeps the quotations. One element is

and the other is

So in script B if I just set an array to the input:

myArray=("$@")

then the unwanted split occurs.
But with eval it works as desired.
Am I missing something?
I was playing with the idea of substituting the quotations for other characters, and then converting the spaces between each set of quotations to yet another character! Messy.
I understand the danger of eval but for my purpose this shouldn't be a issue.

---------- Post updated at 03:14 PM ---------- Previous update was at 02:02 PM ----------

This site helps explain the details of why eval is necessary.

http://fvue.nl/wiki/Bash:_Why_use_eval_with_variable_expansion%3F

I'm still not convinced you need eval, you're not using arrays correctly. If you can possibly avoid using eval, you'll be better off for it -- it's finicky and dangerous.

That's no spoon array, this is an array: myArray=( "$@" )

You can get its contents out splitting on index instead of spaces too: "${myArray[@]}"

It'd be nice if you show the half of the code that's sending your string, not just the half that's receiving it. Both need fixing; you shouldn't need to pass literal quotes into anything.

Thanks for the error posted previously. Was rushed.
Here's 2 scripts. I'm certain my array use is correct.
proofTstA.sh

#!/bin/bash  -e
listOfParms=$(echo ONE TWO \"TH REE\")
echo PARMS: $listOfParms
# next line doesn't work as intended
./proofTstB.sh $listOfParms
# next line does work as intended
eval "./proofTstB.sh $listOfParms"
exit

proofTstB.sh

#!/bin/bash 
echo "------------------------"
echo
parmsArray=("$@")
elem_cnt=${#parmsArray[@]}
echo element count: $elem_cnt
indx=0
while [ "$indx" -lt "$elem_cnt" ]
do
	echo "parm[$indx]: ${parmsArray[indx]}"
	let indx+=1
done
exit

listOfParams wasn't an array either; your arguments weren't ever three things.

#!/bin/bash

listOfParms=( ONE TWO "TH REE" )

# echo really is getting ONE TWO "TH REE" as three args here -- it's just
# impossible to tell from the output.
echo PARMS: "${listOfParms[@]}"

# This illustrates what happens to the array better.
# IFS controls what characters variables split on when evaluated,
# and what character arrays are joined on when flattened with [*].
OLDIFS="$IFS" ; IFS=","
echo PARMS: "${listOfParms[*]}"
IFS="$OLDIFS"

./prooftstb.sh "${listOfParms[@]}"

Your prooftstb.sh works, though you can simplify it:

#!/bin/bash

echo "$# args"

for ((N=1; N<=$#; N++))
do
        echo "arg ${N}:        ${!N}"
done
$ ./prooftsta.sh
PARMS: ONE TWO TH REE
PARMS: ONE,TWO,TH REE
3 arguments
arg 1:        ONE
arg 2:        TWO
arg 3:        TH REE

I never said the input starts out as an array or an array of 3 things. I said it is a list which I could place into a variable. And the list contains quoted strings containing white space.
I have no control over the generation of the input - but of course I can massage it within scriptA before sending it on to scriptB. I merely tried to emulate the input with an echo of that variable. Your IFS block of code is useful. It demonstrated that my "emulated" input was incorrect in regards to what I was receiving during my earlier observations.
With your IFS block I saw the following printout:

Obviously by placing that input into an array - it will contain four elements. (I tried it anyways.) But to get the desired interpretation of this list as 3 elements, I still believe "eval" is necessary.
So thank you for your input. It was very helpful.

Yeah, to process that kind of string you need eval. Just watch what you feed into it very carefully.

STR=$(echo stuff) won't help; the number of arguments is only determined by the splitting of the string later.

You can just do

STR='this has "quotes and stuff" in it'