Search code examples
bashshell

Get current directory or folder name (without the full path)


How could I retrieve the current working directory/folder name in a bash script, or even better, just a terminal command.

pwd gives the full path of the current working directory, e.g. /opt/local/bin but I only want bin.


Solution

  • No need for basename, and especially no need for a subshell running pwd (which adds an extra, and expensive, fork operation); the shell can do this internally using parameter expansion:

    result=${PWD##*/}          # to assign to a variable
    result=${result:-/}        # to correct for the case where PWD is / (root)
    
    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.
    

    Note that if you're applying this technique in other circumstances (not PWD, but some other variable holding a directory name), you might need to trim any trailing slashes. The below uses bash's extglob support to work even with multiple trailing slashes:

    dirname=/path/to/somewhere//
    shopt -s extglob           # enable +(...) glob syntax
    result=${dirname%%+(/)}    # trim however many trailing slashes exist
    result=${result##*/}       # remove everything before the last / that still remains
    result=${result:-/}        # correct for dirname=/ case
    printf '%s\n' "$result"
    

    Alternatively, without extglob:

    dirname="/path/to/somewhere//"
    result="${dirname%"${dirname##*[!/]}"}" # extglob-free multi-trailing-/ trim
    result="${result##*/}"                  # remove everything before the last /
    result=${result:-/}                     # correct for dirname=/ case