12

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?

18

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"
  • 2
    Also basename ~+ – Steven Penny Nov 26 '15 at 19:05
  • you can also do echo "${PWD##*/}" – AMIC MING Jul 19 '16 at 0:05
  • 1. what is -e directory 2. "${PWD##*/}" how does this work? – Sonique Aug 9 '18 at 12:18

protected by user364819 Jun 15 '17 at 8:17

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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