dylanaraps / pure-sh-bible

📖 A collection of pure POSIX sh alternatives to external processes.
MIT License
6.45k stars 281 forks source link

basename #32

Open mmessmore opened 3 years ago

mmessmore commented 3 years ago

For usage messages and such in my shell scripts I typically use prog=${0##*/} instead of prog=$(basename $0).

dirname can similarly be replace with ${var%/*}.

You noted the ''#' and '%' string operations, but I though you may want to call out the opportunity to avoid firing off a process.

Excellent work. I'm sharing with the folks on my team newer to shell, as it is chock full of examples I have learned and taught myself back in the day when I had to write resource efficient scripts than ran on a bunch of Unixes and POSIX(ish) shells.

GeNTooFReaK commented 3 years ago

oh, this is a great hint! I'm using basename and dirname a lot and did not even thought of that simple solution. Thanks!

I vote for this to be mentioned in the sh bible.