Rob HudsonThu Dec 8 17:15:00 PST 2005
Thanks for the description. This helped, and also I found an IBM article that explains the chopping stuff too: http://www-128.ibm.com/developerworks/linux/library/l-bash.html Thanks all, Rob Allen Brown wrote: > Rob Hudson wrote: >> Looks like I can do: >> >> path = `dirname $path` # for directory >> path = `basename $path` # similar to path=${path##/*/} >> >> Is this the best option? > > That is a good option. It provides backward compatibility > with the boring shell (Bourne). However I prefer the $() > syntax since it provides for recursion. It is also a lot > more graceful about quotes. > path="$(dirname "${path}")" > > But to answer your original question > directory="${path%/*}" > > The % means to chop stuff off the end of the path envariable. > The pattern to chop off is /*, which means a slash followed > by anything. Since we only used one %, it chops off the > minimum that matches the pattern. If we had used %% instead, > it would chop off the maximum, resulting in the empty string. > Finally, by enclosing it in quotes we allow for paths with > blanks in them. (Blanks are rather rude, but some people use > them, especially people with a Windoze background.) > > The example you provided is fragile. By starting with a / > it only extracts the basename if the path starts with a /. > So relative paths would fail to extract. > > But lets use something similar > as a start on a different approach. First extract the filename > and then use that to extract the directory. > filename="${path##*/}" > Remove everything up to and including the first slash. > directory="${path%/${filename}}" > Remove from path $filename and its preceding slash. Notice > that since there are no wild cards in this path it doesn't > matter if we use % or %%. > > One thing I don't know about these competing solutions > is whether the latter ones are faster than using