Making cd scripts Work Everywhere With a Bash Function
If you spend a lot of time in the terminal, you probably have a favorite folder you’re constantly jumping into. For many people, it’s a scripts directory where all the handy utilities live. Typing cd ~/scripts every time gets old fast, so why not make cd scripts itself behave like a shortcut?
Aliases won’t work here, because cd is a shell builtin. Instead, you can override cd with a function while still keeping the normal behavior for everything else.
Add this to your ~/.bashrc:
cd() {
if [ "$1" = "scripts" ]; then
builtin cd "$HOME/scripts"
else
builtin cd "$@"
fi
}
Now, typing cd scripts will always jump straight into your ~/scripts folder, no matter where you are in the filesystem. For all other cases, cd behaves exactly as it normally would.
Make sure the directory exists first:
mkdir -p ~/scripts
This approach is portable across different systems, whether you’re on your home Linux box, macOS, or WSL because it references the scripts directory in your home folder, not a hardcoded absolute path.
