I was writing a script and wanted to get the current directory name without the path.

Heres a few ways to do this in Bash.


# Simple way to print it
echo "${PDW##*/}"

# More complicated but more reliable in case of weird directory names
printf '%s\n' "${PWD##*/}"

# Assign to a variable
result=${PWD##*/}


Related External Links: