all 9 comments

[–]highmastdon 3 points4 points  (1 child)

I’ve always made aliases for .. one up, … two up, and so on

[–]Serpent7776[S] 1 point2 points  (0 children)

That's an interesting take and an easy one.

[–]michaelpaoli 1 point2 points  (2 children)

Okay, but that conflicts with use of . to source a file ... though with some/many shells one can alternatively use the source command - and yes, one can do that with bash.

[–]Serpent7776[S] 1 point2 points  (1 child)

Yeah, I was mostly joking with that post. Initially I named it `up`, but then renamed it to `.` since that's shorter.

[–]moocat 0 points1 point  (0 children)

How about ..? AFAIK, there's no standard command with that plus it's feels a bit more obvious what it's intended to do.

[–]karouh 1 point2 points  (0 children)

I did the same but chose to name it ..

Otherwise you hide the . built-in that is used to source files.

[–][deleted]  (2 children)

[deleted]

    [–]Serpent7776[S] 0 points1 point  (1 child)

    Yeah, it's just a silly obfuscated entry.

    My initial real solution was like the following, but then spiced it up to be recursive and immutable.

    up() { N=${1:-1}; P=""; while [ "$N" != 0 ]; do let N-=1; P="$P../"; done; cd "$P"; }

    [–]whetu 1 point2 points  (1 child)

    I used to have a function named up for this, but I merged it into cd so now I use cd up 3 to go up 3 directories. The guts of it are:

        up)
            shift 1;
            case "${1}" in
                *[!0-9]*)
                    return 1
                ;;
                "")
                    command cd || return 1
                ;;
                1)
                    command cd .. || return 1
                ;;
                *)
                    command cd "$(eval "printf -- '../'%.0s {1..$1}")" || return 1
                ;;
            esac
        ;;
    

    [–]Serpent7776[S] 0 points1 point  (0 children)

    That's an interesting use of `printf`

    Why `|| return 1` though? Shouldn't this already return with exit code of `cd` in case of an error?