Skip to content

Instantly share code, notes, and snippets.

@haeramkeem
Created April 22, 2022 15:04
Show Gist options
  • Save haeramkeem/72761bfb7dc455f222956e59c375e8b6 to your computer and use it in GitHub Desktop.
Save haeramkeem/72761bfb7dc455f222956e59c375e8b6 to your computer and use it in GitHub Desktop.
Get current directory name only
#!/bin/bash
# https://stackoverflow.com/a/1371283
result=${PWD##*/} # to assign to a variable
printf '%s\n' "${PWD##*/}" # to print to stdout
# ...more robust than echo for unusual names
# (consider a directory named -e or -n)
printf '%q\n' "${PWD##*/}" # to print to stdout, quoted for use as shell input
# ...useful to make hidden characters readable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment