Bash get basename of filename or directory name

Originally published at: https://www.cyberciti.biz/faq/bash-get-basename-of-filename-or-directory-name/

I need to extract file basename in bash running on Linux. How can I use bash to get basename of filename or directory name for given path?

basename xx/yy/zz = zz
dirname xxx/yy/zz = xxx/yy