aboutsummaryrefslogtreecommitdiff
path: root/sh/shrc.d/pd.sh
blob: d5257ba5a98a28030eea7d0dd5bf8ee39029f9b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Attempt to change into the argument's parent directory; This is intended for
# use when you've got a file path in a variable, or in history, or in Alt+.,
# and want to quickly move to its containing directory. In the absence of an
# argument, this just shifts up a directory, i.e. `cd ..`
#
# Note this is equivalent to `ud 1`.
pd() {

    # Check arguments; default to $PWD
    if [ "$#" -gt 1 ] ; then
        printf >&2 'pd(): Too many arguments\n'
        return 2
    fi
    set -- "${1:-"$PWD"}"

    # Make certain there are no trailing slashes to foul us up, and anchor path
    # if relative
    while : ; do
        case $1 in
            */) set -- "${1%/}" ;;
            /*) break ;;
            *) set -- "$PWD"/"$1" ;;
        esac
    done

    # Strip a path element
    set -- "${1%/*}"

    # Try to change into the determined directory, or root if empty
    # shellcheck disable=SC2164
    cd -- "${1:-/}"
}