# usage: basename "path" ["suffix"]
# dir=${1%${1##*[!/]}}
basename() {
  dir=${1%"${1##*[!/]}"}
  dir=${dir##*/}
  dir=${dir%"$2"}
  printf '%s\n' "${dir:-/}"
}

# _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##*[!/]}}
#   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:-/}"
# }

# $ basename ~/Pictures/Wallpapers/1.jpg
# 1.jpg

# $ basename ~/Pictures/Wallpapers/1.jpg .jpg
# 1

# $ basename ~/Pictures/Downloads/
# Downloads
