Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm sorry if this is too simple or has been covered before... but I hope someone here can help out. I can't figure out how to expand the * wildcard in a pathname unless it is followed by a space. For example,

echo $path* $dir


/Data0001 /subfolder 

which is what I want, but without the space. If I try to take out the space with,

echo $path*$dir

I get,


Any tips on how to get the * to expand without the whitespace?


share|improve this question

Put the variable name(s) in curly braces, e.g.:

echo ${path}*${dir}
share|improve this answer
That still returns /Data*/subfolder – M Fero Aug 21 '12 at 9:12

It's doing pattern matching on the entire word, not just the ${path}* part -- so if $path was "/" and $dir was "/subdirname", it would look for matches to /*/subdirname, which essentially looks for directories in / that contain subdirectories named subdirname. If you want it to find directories that don't already contain /subdirname, you have to leave that out of pattern, and add it later. Here's an example that stores the matches in an array, then adds the subdirectory:

echo "${matchedDirs[@]/%/$dir}"

If that final construct is hard to make out, the [@] part means "all elements of the array, each treated as a separate word", and /%/$dir means "replace the end (of each element) with $dir" (i.e. append $dir to each array element).

share|improve this answer
With my setup I only expect path* to match one directory, but still this does the job. Thanks! – M Fero Aug 21 '12 at 9:28
Great! Since it did the job, please mark it as the accepted answer. – Gordon Davisson Aug 21 '12 at 13:48

Change it to

dir=/Data sub=subfolder
echo $dir*/$sub


dir=/Data* sub=subfolder
echo $dir/$sub

would also work

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.