These 3 simple bash variables add bookmarks and back buttons to your Linux terminal session

These three Bash features turn the terminal into something closer to a file browser, with saved locations, a quick way back, and a full history of where you’ve been.None of them need a plugin, an alias framework, or a new shell.Add How-To Geek on Google Add as Preferred Source Follow on Google Discover The bookmark system: CDPATH Jump to project folders from anywhere Close Most people treat cd as a command that only understands two kinds of paths: absolute ones that start at the root, and relative ones that start wherever you are.

CDPATH adds a third option.It’s a shell variable holding a colon-separated list of directories, and when you type cd followed by a name that doesn’t begin with a slash, a dot, or two dots, Bash checks each directory in that list for a matching subfolder before giving up.Say you keep your work in ~/projects and your notes in ~/Documents/writing.

Adding CDPATH=.:~/projects:~/Documents/writing to your ~/.bashrc means typing cd website takes you straight to ~/projects/website from any location on the system, as long as there’s no folder called website in your current directory.The leading dot matters.It tells Bash to check the current directory first, which keeps ordinary relative navigation working the way you expect.

An empty entry at the start of the list does the same thing, but the dot is easier to read.When Bash resolves a name through CDPATH rather than the current directory, it prints the full path it landed in.That small confirmation is useful, because it tells you a bookmark was used rather than a local folder.

Keep in mind one thing, though: set the variable in ~/.bashrc without exporting it.An exported CDPATH is inherited by scripts, and a script that runs cd build expecting a local folder can end up somewhere else entirely if one of your bookmarked directories also contains a build folder.Keeping it local to your interactive shell avoids that whole class of bugs.

Paired with the bash-completion package, which understands CDPATH, tab completion will also suggest folders from your bookmarked locations, so you rarely have to remember exact names.The back button: cd - Toggle between your two most recent directories Every time you change directories, Bash quietly records where you just were in a variable called OLDPWD.Running cd - reads that variable and takes you back to it.

It’s effectively shorthand for cd "$OLDPWD" followed by printing the destination, which is why the command echoes the path after it runs.The behavior that makes it so handy is that it toggles.When you return to the previous directory, Bash updates OLDPWD to point at the place you just left.

Running cd - a second time sends you back again.That makes it ideal for the very common pattern of bouncing between two locations, such as a source directory and a log folder, or a configuration directory under /etc and the project you’re actually editing.You don’t need to type either path more than once.

Because OLDPWD is an ordinary variable, you can also use it outside of cd.Copying a file into the directory you just left is as simple as cp report.txt "$OLDPWD", and listing its contents works with ls "$OLDPWD".Bash also offers a tilde shortcut, ~-, which expands to the same value and saves a few keystrokes, so cp report.txt ~- does the same job.

The limitation is built into the design.cd - remembers exactly one previous location, not a history.If you move from your home folder to a project, then into a subfolder, then run cd -, you’ll return to the project, not your home folder, and running it again just brings you back to the subfolder.

For quick back-and-forth work, that’s precisely what you want.Once your path through the file system involves three or more stops you want to revisit, it stops being the right tool, and that’s where the directory stack comes in.The navigation history stack: pushd and popd Keep a full trail of where you’ve been And now, let's address that last limitation I mentioned just above.

Where cd - remembers one location, pushd and popd remember as many as you like.Bash maintains a list called the directory stack, exposed through the DIRSTACK array variable, and these two commands add to it and remove from it.Running pushd /var/log changes into that directory and places it on top of the stack, while the directory you came from stays underneath.

Running popd removes the top entry and returns you to the one below it.That gives you real backtracking.You can push your way through a project folder, a system directory, and a backup drive, then pop your way back out in reverse order without retyping a single path.

After each operation, both commands print the current stack, which helps you keep track of where you are.If that output gets noisy, sending it to /dev/null silences it.The dirs command shows the stack on demand, and dirs -v lists each entry with a numeric index.

Those indices unlock the more powerful features.Running pushd +2 rotates the stack so the third entry comes to the top and changes into it, letting you jump directly to any saved location rather than unwinding step by step.Tilde expansion understands the stack too, so ~1 refers to the second entry, which means a command like cp file.txt ~2 can target a stacked directory without moving there.

Running pushd with no arguments swaps the top two entries, behaving much like cd -.When the stack has served its purpose, dirs -c clears it.Some people go further and alias cd to pushd so every move is recorded automatically, though that tends to build an unwieldy stack quickly.

Using pushd deliberately, for the locations you know you’ll return to, usually works better.Smarter terminal navigation with built-in Bash tools CDPATH saves your favorite locations, cd - handles quick round trips, and the directory stack keeps a full trail when you need it.Together they cover nearly every navigation pattern, and each one takes seconds to start using.

Read More
Related Posts