Last active
July 23, 2018 06:31
-
-
Save Bios-Marcel/ac785e0f52e8e494ec6e06653ceecc9f to your computer and use it in GitHub Desktop.
function that adds the capability to go back to any directory named X using cd. Just put it into ~/.bashrc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cdb() { | |
#suppress all output and run the original cd with all given arguments | |
command cd $@ &>/dev/null | |
#If the original ran successfully, do an early exit | |
if [ $? -eq 0 ]; then | |
return | |
fi | |
START_DIR="$PWD" | |
while [[ $PWD != '/' && $PWD != **"/$1" ]]; do | |
command cd ..; | |
done | |
if [ "$PWD" = '/' ]; then | |
command cd "$START_DIR" | |
command cd "$1" | |
fi | |
} |
Instead of having if
-branches for every edgecase, i could just run cd with the given input and only apply the special behaviour on failure.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug:
cd
doesn't work anymore, instead you have to do:
I'll have to fix that.