23

I know that pwd will print the full path of current working directory, but I want to print only the name of the directory. Is there a simple command to do this without to parse pwd?

1 Answer 1

31

Yes, there is. You can use pure bash:

echo "${PWD##*/}"

or better, to avoid the case when you could be in -e directory:

printf '%s\n' "${PWD##*/}"

(thanks to @gniourf_gniourf for the second suggestion).

Or you can use basename tool:

basename "$PWD"
4
  • 2
    Also basename ~+
    – Zombo
    Nov 26, 2015 at 19:05
  • you can also do echo "${PWD##*/}"
    – AMIC MING
    Jul 19, 2016 at 0:05
  • 2
    1. what is -e directory 2. "${PWD##*/}" how does this work?
    – Sonique
    Aug 9, 2018 at 12:18
  • I prefer basename `pwd` Jul 22 at 21:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.