[last updated - 05 August 2003]
The basename utility is used for stripping off path names. Try the following command and you will see how it works:
basename /aa/bb/ccc.sas
You will see that ccc.sas is returned which is what you want. You will often see it in scripts enclosed in the do-it-now quotes like this:
prog=`basename $file`
It is fine for single instances like that. But you should not come to the conclusion that it is the only way to strip a path name off. You can also do it with sed. Try this:
echo /aa/bb/ccc.sas | sed 's%.*/%%'
sed will do this for a stream of input like this:
ls -1 ./sasmacros/*.sas | sed 's%.*/%%'
This is the technique you should use for chopping the path names off lists of things. To do the same with basename you would have to use this command:
ls -1 ./sasmacros/*.sas | xargs -i{} basename {}
The above will work but will take many times longer to run compared to using sed. This is because basename's argument must follow it. What the above is doing is telling xargs to insert what it receives from standard input where it finds the "{}" pattern. In other words, after basename.
To sum up, basename has its place for stripping off path names, but it should only be used on single items. Use sed for multiple items.
Go back to the home page.
E-mail the macro and web site author.