Difference between cd - vs cd ~-
Yesterday I posted one of my old (16 years ago) blog entries on twitter, Backtracking with Bash which shows a cool trick you can use to go to the directory you were previously in using
cd ~- for example:
$ cd /var $ cd /etc $ cd ~- $ pwd /var
Ryan Guill pointed out that
cd - also works, so what is the difference between
cd ~- and
$ cd /var $ cd /etc $ cd - /var $ pwd /var
So what is the difference?
The biggest difference between
cd ~- and
cd - is that
~- can be used in any command because it is part of the shells tilde expansion. The
- shortcut can only be used with the
So for example if your directory was previously
/var/log/apache2 and you want to tail the
access_log file in there you can just
$ cd /var/log/apache2 $ cd /etc $ tail ~-/access_log It Works... $ tail -/access_log tail: illegal option -- /
The second difference is that
cd - will print the directory it changed to out to standard output, and
cd ~- just changes directories without printing anything.
Like this? Follow me ↯Tweet Follow @pfreitag
Difference between cd - vs cd ~- was first published on September 19, 2019.
If you like reading about bash, linux, or cd then you might also like:
- Creating a Symbolic Link with ln -s What Comes First?
- Bash Loop To Wait for Server to Start
- Counting IP Addresses in a Log File
- Recursively Counting files by Extension on Mac or Linux
- Passing Environment Variables to Sudo Command
- The 15 Essential UNIX commands
- Linux and OS X Shell Commands
- Counting files