basename
The command that strips the directory part from a path and prints only the last name, as in basename /tmp/work gives work.
In more detail
dirname does the opposite and keeps only the directory part. In bash, ${path##*/} does the same as basename without starting a program.
Example
here=$(basename "$(pwd)")Related terms
- prefix and suffix removal — The ${name#pattern} and ${name%pattern} forms, which remove a matching glob pattern from the start or the end of a value.
- file path — A string naming the location of a file, made of directory names separated by forward slashes.
Taught in
- Style That Prevents Bugs — Bash Scripting & Command Line Automation