Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

basename #32

Open
mmessmore opened this issue Oct 20, 2020 · 1 comment
Open

basename #32

mmessmore opened this issue Oct 20, 2020 · 1 comment

Comments

@mmessmore
Copy link

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
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants