r/commandline • u/Serpent7776 • Oct 21 '23
Recursive bash function to replace cd ../
This function replaces cd ..:
. 1 works like cd .., . 2 works like cd ../../ and so on. . works like . 1.
.() { , ${1:-1}; }; ,() { local N=$(($1-1)) C=${1/#0*/cd} D=${1/#[1-9*]/../}; ${C/#[1-9]*/,} ${N/-1/} ${2}${D/#0*/} ;}
4
Upvotes
5
u/highmastdon Oct 21 '23
I’ve always made aliases for .. one up, … two up, and so on
2
u/michaelpaoli Oct 21 '23
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.
2
u/whetu Oct 22 '23
I used to have a function named
upfor this, but I merged it intocdso now I usecd up 3to go up 3 directories. The guts of it are:2
u/karouh Oct 22 '23
I did the same but chose to name it ..
Otherwise you hide the . built-in that is used to source files.
0
u/[deleted] Oct 22 '23
[deleted] — view deleted comment