Article 73XYM Shell variable ~-

Shell variable ~-

by
John
from John D. Cook on (#73XYM)

After writing the previous post, I poked around in the bash shell documentation and found a handy feature I'd never seen before, the shortcut ~-.

I frequently use the command cd - to return to the previous working directory, but didn't know about ~- as a shotrcut for the shell variable $OLDPWD which contains the name of the previous working directory.

Here's how I will be using this feature now that I know about it. Fairly often I work in two directories, and moving back and forth between them using cd -, and need to compare files in the two locations. If I have files in both directories with the same name, say notes.org, I can diff them by running

 diff notes.org ~-/notes.org

I was curious why I'd never run into ~- before. Maybe it's a relatively recent bash feature? No, it's been there since bash was released in 1989. The feature was part of C shell before that, though not part of Bourne shell.

The post Shell variable ~- first appeared on John D. Cook.
External Content
Source RSS or Atom Feed
Feed Location http://feeds.feedburner.com/TheEndeavour?format=xml
Feed Title John D. Cook
Feed Link https://www.johndcook.com/blog
Reply 0 comments