I'm trying to echo a combination of text and variables containing wildcards that I need "unpacked", but I encountered the following behavior:

If I say:

echo "something:" $FILENAME

I get:

something: somefile003.txt

Which is what I want, but if I say: If I say:

echo "something:"$FILENAME

I get:


So it seems like if there is no space between quotes and the variable it doesn't glob the wildcard. Is there a way to get it to process the * without adding a space?

  • It does try to parse the wildcard; there are just no files that match the glob something:somefile*.txt.
    – DopeGhoti
    Commented Jul 24, 2017 at 18:09
  • 1
    If you say FILENAME = somefile*.txt your shell will try to execute a program called FILENAME. If you say FILENAME=somefile*.txt your shell will set a variable called FILENAME. Please be precise; it's important. Commented Jul 24, 2017 at 18:11
  • @DopeGhoti I see you've edited the question but how do you know which places really contain a space and which don't, given the question is about the effects seen when there is one or not? Commented Jul 24, 2017 at 18:14
  • The 'what I get' part of the question would be factually incorrect if there had been spaces as FILENAME would not have been assigned to something which could have globbed.
    – DopeGhoti
    Commented Jul 24, 2017 at 18:15
  • Perhaps printf would help: printf "something:%s\n" $FILENAME.
    – jimmij
    Commented Jul 24, 2017 at 18:16

2 Answers 2


with a shell that handles arrays such as bash you can glob into an array, thus


and reference the first element like this

echo "something:${FILENAMES[0]}"

or all of them like this

echo "somethings:${FILENAMES[@]}"

I would strongly recommend that you "double quote" your variables when you use them. This avoids them being expanded into multiple words unexpectedly.


Try to define your variable like this:

FILENAME=\ somefile*.txt; # that is, with a leading space ... and then
echo "something:"$FILENAME; 

this gets variable interpolated to... something: somefile*.txt

then this gets wildcard expanded to... something: somefile003.txt

these two arguments then get passed to echo which promptly takes them stdout.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .