Discussion

cd –, a shell feature I didn't know about

Recently, someone submitted a pull request to one of my projects, which included a shell feature, i always wanted to have, but didn't know it exists. The project includes a little build script, which changes the directory, executes some commands and then changes back to the original directory, where it came from.

To achieve this, i used the "pushd" and "popd" command, which unfortunately seems not to be available on every POSIX platform. So, as an alternative, the pull request included:

  cd -
which i never came across and which surprisingly (at least for me) did the same thing... Further research lead me to following information:

"cd -" expands the "-" to $OLDPWD, which, as the name intends, points to the directory visited before. Using this variable results in a history size of 1, so you can only switch between the last and the current directory, but in most cases, that should be enough.

Then I immediately thought of a problem... how to change to a directory named "-"? Well, heres the answer:

  cd ./-
  
Now that I know about it, I use it nearly every day.

Have fun!

7 points sandreas 7 years ago 5 comments

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton