Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

yields

/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,

/Data*/subfolder

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

Thanks.

share|improve this question

3 Answers 3

Change it to

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

.

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

would also work

share|improve this answer

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:

path="/"
dir="/subdirname"
matchedDirs=("$path"*)
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

Your Answer

 
discard

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.