diff --git a/README.md b/README.md index c22b3f5..deb2938 100644 --- a/README.md +++ b/README.md @@ -1067,10 +1067,9 @@ Alternative to the `dirname` command. dirname() { # Usage: dirname "path" dir=${1%%/} + [[ "${dir##*/*}" ]] && dir=. dir=${dir%/*} - [[ $1 == */* ]] || dir=. - printf '%s\n' "${dir:-/}" } ``` diff --git a/manuscript/chapter5.txt b/manuscript/chapter5.txt index 84b9e2b..e602a67 100644 --- a/manuscript/chapter5.txt +++ b/manuscript/chapter5.txt @@ -10,10 +10,9 @@ Alternative to the `dirname` command. dirname() { # Usage: dirname "path" dir=${1%%/} + [[ "${dir##*/*}" ]] && dir=. dir=${dir%/*} - [[ $1 == */* ]] || dir=. - printf '%s\n' "${dir:-/}" } ``` diff --git a/test.sh b/test.sh index debf2da..597de2b 100755 --- a/test.sh +++ b/test.sh @@ -129,6 +129,9 @@ test_dirname() { result="$(dirname "/foo/foo")" assert_equals "$result" "/foo" + + result="$(dirname "something/")" + assert_equals "$result" "." } test_basename() {