1. Overview
Typically, we might use the cd command for changing the current working directory. However, let’s consider the case that we’re running some commands in the terminal, and then we want to navigate to the previous directory we were in.
It could be that we can’t remember the previous path, or we may know the path, but we want to go back quickly. In this tutorial, we’re going to see how we can use – as an argument of cd to accomplish this.
2. The Meaning of – With cd
A single dash as an argument for different commands may have different meanings. For instance, using a dash with a command like paste may lead to reading the standard input. But, using a dash as an argument of cd will change the current directory to the previous working directory.
Let’s suppose we’re at /home/pi directory:
$ pwd
/home/pi
Let’s change the current directory to /home:
$ cd /home
Now, let’s use cd – to see what it does:
$ cd -
/home/pi
When we use a single dash as an argument of the cd command it contains the path of the previous working directory (OLDPWD). So, we could quickly get back to where we were (/home/pi).
Note that we can print the value of OLDPWD at any time we want:
$ echo "$OLDPWD"
/home
The OLDPWD variable is set by cd. Now, let’s use cd a few times:
$ cd /
$ cd /etc
$ cd /var
$ cd lib
If we use pwd command, we can see that we’re in /var/lib directory. So, the OLDPWD value would be /var. Now let’s use cd – a couple of times and see the outputs:
$ cd -
/var
$ cd -
/var/lib
$ cd -
/var
$ cd -
/var/lib
We can see that no matter how many times we use cd –, it’s not possible to go back to the first and second directories (/etc and / directory) because OLDPWD is just a variable, not a stack. So, it only contains one value at any given time. Therefore, we’re just toggling between the two last directories. To have a stack of the previous directories, we can consider using the pushd and popd commands.
Note that if we use cd – right after the system boots, we’ll get an error:
$ cd -
-bash: cd: OLDPWD not set
That’s because the OLDPWD is not set yet. Also, note that this command will return an exit status of 0 if the process is successful or 1 if the process is unsuccessful:
$ cd -; echo $?
-bash: cd: OLDPWD not set
1
We can use $? which is a special parameter in Linux that contains the exit status of the most recently executed command in the foreground.
3. Equivalent Command to cd –
Notice in the previous examples, we may see that the cd – command is printing the current working directory, in addition to changing the directory. Therefore, we can say that it is equal to the command:
$ cd "$OLDPWD" && pwd
We’re splitting the function of cd – into two parts:
- Changing the directory to OLDPWD
- Printing the current working directory
Since the cd command does not print the current working directory, after changing the directory to OLDPWD, we use the pwd command to also print the current working directory.
Of course, if we don’t want cd – to print the current working directory, we can use:
$ cd "$OLDPWD"
4. Conclusion
In this article, we saw how to use cd – to go back to our previous working directory. As we have explained the dash in cd – contains the OLDPWD value.