diff --git a/README.md b/README.md index 88695a9..5569b9b 100644 --- a/README.md +++ b/README.md @@ -1101,8 +1101,11 @@ Alternative to the `basename` command. ```sh basename() { # Usage: basename "path" - : "${1%/}" - printf '%s\n' "${_##*/}" + dir=${1%${1##*[!/]}} + dir=${dir##*/} + dir=${dir%"$2"} + + printf '%s\n' "${dir:-/}" } ``` @@ -1112,6 +1115,9 @@ basename() { $ basename ~/Pictures/Wallpapers/1.jpg 1.jpg +$ basename ~/Pictures/Wallpapers/1.jpg .jpg +1 + $ basename ~/Pictures/Downloads/ Downloads ``` diff --git a/manuscript/chapter5.txt b/manuscript/chapter5.txt index 754a930..fce45ed 100644 --- a/manuscript/chapter5.txt +++ b/manuscript/chapter5.txt @@ -40,8 +40,11 @@ Alternative to the `basename` command. ```sh basename() { # Usage: basename "path" - : "${1%/}" - printf '%s\n' "${_##*/}" + dir=${1%${1##*[!/]}} + dir=${dir##*/} + dir=${dir%"$2"} + + printf '%s\n' "${dir:-/}" } ``` @@ -51,6 +54,9 @@ basename() { $ basename ~/Pictures/Wallpapers/1.jpg 1.jpg +$ basename ~/Pictures/Wallpapers/1.jpg .jpg +1 + $ basename ~/Pictures/Downloads/ Downloads ```