I have created the following two minimal shell scripts...


#!/usr/bin/env bash


for letter in $letterList
    echo $letter

and a second


#!/usr/bin/env bash


test1 $letterList

When I execute test1 'A B C' I get the desired output, A, B and then C.

But when I execute test2 'A B C' I expect test1 'A B C' to happen, but the actual output is only A.

So it appears as though the nested variable list is not somehow being read – in other words, what is being executed is test1 A.

What's going on and how can I achieve the desired result?

P.S.: I'm using OS X, if that's relevant.

  • In test2 replace last line with ./test1 "$letterlist" – kaylum Jan 12 at 10:40
  • @kaylum Many thanks! The addition of " " does the trick. The addition of `.` threw me an error. I assume that it is not necessary since without it it works. – Geoff Jan 12 at 10:46

When ./test2 'a b c' is run we get:

$1=a b c

So the last line becomes:

test1 a b c

And when test1 runs we get:


That is, the args give to test1 are not enclosed in quotes and hence they become three args and not one.

To pass test1 a single arg just enclose the variable in double quotes. That is, replace the last line in test2 with:

./test1 "$letterlist"

Note, I have also added ./ to give the path of the executable as it is not good practice to have . in $PATH.

|improve this answer|||||
  • It is possible that the OP has placed test1 and test2 in /usr/bin, /usr/local/bin, $HOME/bin, or some other directory that is in his PATH. In that case, prepending ./ is not only not constructive, but rather, harmful (as the OP reports). Of course I agree that . should not be in $PATH. – G-Man Says 'Reinstate Monica' Jan 26 at 20:26

You have no list variable in your code. You have a string.

The call to test1 uses the expansion $letterList unquoted. This will make the shell split its value up into multiple words on spaces, tabs and newline characters (by default), and each word will undergo filename globbing (if they contain filename globbing characters). The resulting words are passed to test1 as separate arguments.

In test1, you only put the very first of these arguments into letterList.

If you want to pass the string in $letterList as it is to test1 from test2, you need to double quote its expansion in the call:

test1 "$letterList"

Incidentally, you rely on the shell performing the splitting of the $letterList value in your loop in test1 (the same mechanics that cause your issue), but you never protect the code from accidentally performing filename globbing (test your script with e.g. test2 "A B C *" to see what I mean).

If you want to pass a list rather than a single string, then call test2 with a list rather than with a string:

test2 A B C "Hello World" "* *"

Then, in test2, pass the arguments to test1:


test1 "$@"

Here, "$@" will expand to the list of positional parameters (the arguments to the script), with each individual parameter being quoted. This means that an argument like Hello World will not be split into multiple arguments in the call to test1, and the string * * will also not get split up into two and no filename globbing will occur.

In test1, iterate over the arguments:


for arg in "$@"; do
    printf '%s\n' "$arg"



for arg do
    printf '%s\n' "$arg"

... or, if you just want to print the arguments, just


printf '%s\n' "$@"

I've used /bin/sh as the shell throughout as there is nothing here that is specific to bash.

|improve this answer|||||

It’s not clear what your actual objective is.  The only thing you have said clearly is that you want test2 'A B C' to output A, B and C on three separate lines.

Here is a different solution that will give you that result.  In general, it seems to give the same output as kaylum’s answer.

  • Keep test2 the same.
  • Change test1 as follows:

    for letter in $*
        printf '%s\n' "$letter"

    This, naturally, changes the behavior of test1.  It now looks at all its arguments, not just $1.  So test1 'A B C' 'D E F' will output A, B, C, D, E and F on six separate lines (instead of just A, B and C).

We normally discourage the use of $* because it breaks strings that contain spaces into separate words.  For example, "lazy dog" would become two words (arguments).  But that seems to be exactly the behavior you want here.

printf is slightly safer than echo in the case that a filename begins with - or contains \.  That’s been discussed at length on this site; if you want more information, just search for it.

|improve this answer|||||

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.