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!
5 comments
[ 2.9 ms ] story [ 26.9 ms ] threadA similar one is
!!
For example
And you forgot sudo, whoops !! will be replaced by the last command you executed.Or
Oh whoops, forgot cat/vim/less/tail/whatever Etc