diff options
-rw-r--r-- | README.md | 43 |
1 files changed, 42 insertions, 1 deletions
@@ -527,10 +527,34 @@ Alternative to the `dirname` command. ```sh dirname() { # Usage: dirname "path" - dir=${1%%/} + + # If '$1' is empty set 'dir' to '.', else '$1'. + dir=${1:-.} + + # Strip all trailing forward-slashes '/' from + # the end of the string. + # + # "${dir##*[!/]}": Remove all non-forward-slashes + # from the start of the string, leaving us with only + # the trailing slashes. + # "${dir%%"${}"}: Remove the result of the above + # substitution (a string of forward slashes) from the + # end of the original string. + dir=${dir%%"${dir##*[!/]}"} + + # If the variable *does not* contain any forward slashes + # set its value to '.'. [ "${dir##*/*}" ] && dir=. + + # Remove everything *after* the last forward-slash '/'. dir=${dir%/*} + # Again, strip all trailing forward-slashes '/' from + # the end of the string (see above). + dir=${dir%%"${dir##*[!/]}"} + + # Print the resulting string and if it is empty, + # print '/'. printf '%s\n' "${dir:-/}" } ``` @@ -554,10 +578,27 @@ Alternative to the `basename` command. ```sh basename() { # Usage: basename "path" ["suffix"] + + # Strip all trailing forward-slashes '/' from + # the end of the string. + # + # "${1##*[!/]}": Remove all non-forward-slashes + # from the start of the string, leaving us with only + # the trailing slashes. + # "${1%%"${}"}: Remove the result of the above + # substitution (a string of forward slashes) from the + # end of the original string. dir=${1%${1##*[!/]}} + + # Remove everything before the final forward-slash '/'. dir=${dir##*/} + + # If a suffix was passed to the function, remove it from + # the end of the resulting string. dir=${dir%"$2"} + # Print the resulting string and if it is empty, + # print '/'. printf '%s\n' "${dir:-/}" } ``` |