diff options
author | Dylan Araps <dylan.araps@gmail.com> | 2019-09-24 08:11:57 +0300 |
---|---|---|
committer | Dylan Araps <dylan.araps@gmail.com> | 2019-09-24 08:11:57 +0300 |
commit | dc6de0ef34bcfad6e7673b7e3c5aef58d33ce734 (patch) | |
tree | 520e40f7e1ec22f3c5967056f4b96c90c9a64b85 | |
parent | 303a209161076312c15f9e3912dac6178fbc9114 (diff) | |
download | pure-sh-bible-dc6de0ef34bcfad6e7673b7e3c5aef58d33ce734.tar.gz pure-sh-bible-dc6de0ef34bcfad6e7673b7e3c5aef58d33ce734.zip |
docs: update
-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:-/}" } ``` |