1

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.

3 Answers 3

4

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

echo ${path}*${dir}
1
  • That still returns /Data*/subfolder
    – M Fero
    Commented Aug 21, 2012 at 9:12
1

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).

2
  • With my setup I only expect path* to match one directory, but still this does the job. Thanks!
    – M Fero
    Commented Aug 21, 2012 at 9:28
  • 1
    Great! Since it did the job, please mark it as the accepted answer. Commented Aug 21, 2012 at 13:48
0

Change it to

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

.

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

would also work

You must log in to answer this question.

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