2

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:

FILENAME=somefile*.txt
echo "something:" $FILENAME

I get:

something: somefile003.txt

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

FILENAME=somefile*.txt
echo "something:"$FILENAME

I get:

something:somefile*.txt

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?

DopeGhoti
  • 76,081
Maxim
  • 728

2 Answers2

5

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

FILENAMES=(somefile*.txt)

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.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
1

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.