Last active
June 23, 2021 19:52
-
-
Save fakuivan/639f28af3e0f7476b275df2bd20e764a to your computer and use it in GitHub Desktop.
Try to cd by swapping base by symlink
This file contains hidden or 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
# Append this to your "~/.bashrc" file | |
if LINK="$(~/cdlink.sh ~/SeaDrive)"; then | |
cd "$LINK" | |
echo Changed base path to "${LINK@Q}" | |
fi |
This file contains hidden or 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
#!/usr/bin/env bash | |
set -euo pipefail | |
# Try to cd by swapping base by symlink | |
cdlink () { | |
local relative | |
local previous | |
local base="$1" | |
previous="$(pwd)" | |
relative="$(realpath --relative-base="$base" .)" | |
cd "$base" | |
cd "$relative" | |
if [[ "$(pwd)" != "$previous" ]]; then | |
return 0; | |
fi | |
cd "$previous" | |
return 1; | |
} | |
for symlink in "$@"; do | |
if ! cdlink "$symlink"; then | |
continue | |
fi | |
pwd | |
exit 0 | |
done; | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment