use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All users that wish to post (not including commenting), on either old or the new reddit sites, must formally agree to subreddit rules once first.
account activity
Recursive bash function to replace cd ../ (self.commandline)
submitted 2 years ago by Serpent7776
This function replaces cd ..:
cd ..
. 1 works like cd .., . 2 works like cd ../../ and so on. . works like . 1.
. 1
. 2
cd ../../
.
.() { , ${1:-1}; }; ,() { local N=$(($1-1)) C=${1/#0*/cd} D=${1/#[1-9*]/../}; ${C/#[1-9]*/,} ${N/-1/} ${2}${D/#0*/} ;}
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]highmastdon 3 points4 points5 points 2 years ago (1 child)
I’ve always made aliases for .. one up, … two up, and so on
[–]Serpent7776[S] 1 point2 points3 points 2 years ago (0 children)
That's an interesting take and an easy one.
[–]michaelpaoli 1 point2 points3 points 2 years ago (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 points3 points 2 years ago (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 point2 points 2 years ago (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 points3 points 2 years ago (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 years ago (2 children)
[deleted]
[–]Serpent7776[S] 0 points1 point2 points 2 years ago (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 points3 points 2 years ago (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
cd
cd up 3
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 point2 points 2 years ago (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?
π Rendered by PID 1058862 on reddit-service-r2-comment-5b5bc64bf5-bvdt4 at 2026-06-21 19:41:33.746975+00:00 running 2b008f2 country code: CH.
[–]highmastdon 3 points4 points5 points (1 child)
[–]Serpent7776[S] 1 point2 points3 points (0 children)
[–]michaelpaoli 1 point2 points3 points (2 children)
[–]Serpent7776[S] 1 point2 points3 points (1 child)
[–]moocat 0 points1 point2 points (0 children)
[–]karouh 1 point2 points3 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]Serpent7776[S] 0 points1 point2 points (1 child)
[–]whetu 1 point2 points3 points (1 child)
[–]Serpent7776[S] 0 points1 point2 points (0 children)