Environment & Shell Customization (Aliases/Functions/.Bashrc)
Stop hunting for the directory you just left
🧩 The Challenge
You ever `cd` into a deep directory structure to tweak a config file and then realize five minutes later you have no idea where you are or how you got there? I’ve spent way too much time hitting pwd and staring at long paths when I should have just been working.
💡 The Fix
Use a shell function that keeps a tiny history of your recent locations so you can bounce back without mashing the up arrow or typing out full paths. It’s like a browser back button for your terminal.
cdback() { if [ -n "$OLDPWD" ]; then cd "$OLDPWD"; else echo "No previous directory."; fi; }
alias -- -='cdback'
⚙️ Why It Works
By leveraging the built-in OLDPWD variable that Bash maintains automatically, this function jumps you straight to your last location instantly. You just type a hyphen and hit enter.
🚀 Pro-Tip: Add a pushd/popd alias if you actually need to jump between more than two spots.
Linux Tips & Tricks | © ngelinux.com | 9/15/2026
