aboutsummarylogtreecommitdiffstats
path: root/cd-memo
blob: 6954c0cab8867d9826f19109f4e5fdc4a60b04d7 (plain)
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
33
34
35
#!/bin/bash

if [ -z "$1" ]; then
    set -- "$HOME"
fi

if [ "$1" = "-" ]; then
    \cd - 2> /dev/null

    if [ "$?" -ne "0" ]; then
        echo "cd: previous directory ($OLDPWD) is inaccessible"
        return 1
    fi

    echo $OLDPWD > ~/.cdmem

elif [ "$1" = "--" ]; then
    \cd "$(cat ~/.cdmem)" 2> /dev/null

    if [ "$?" -ne "0" ]; then
        echo "cd: previous directory ($(cat ~/.cdmem)}) is inaccessible"
        return 1
    fi

    cat ~/.cdmem

elif [ -d "$1" ]; then
    echo $(realpath "$1") > ~/.cdmem
    \cd "$1"

else
    echo "cd: no such directory: $1"
    return 1
fi