We consider this article to be an extension to – Change to previous working directory with cd command in Linux. There we used the following command to switch back to previous working directory –
Or, similar outcome can also be achieved through following command –
PWD and OLDPWD are two environment variables in Bash. These special variables store valuable information and are defined for Bash. Issue the following in terminal to see information stored in all environment variables –
In the list, you would see the data stored in PWD and OLDPWD environment variables.
PWD vs OLDPWD – Bash environment variables
It is clear to us that, PWD and OLDPWD are two Bash environment variables. So, they store some data.
PWD stores data about our present working directory in shell whereas, OLDPWD is about the previous working directory.
Let’s say, currently we are working in /home/techpiezo/Downloads directory. The directory location – /home/techpiezo/Downloads gets stored in PWD variable. But, if we do –
Then, data stored in PWD now becomes /dev/shm. But, what about OLDPWD then? As already covered, location of our previous working directory gets stored in OLDPWD environment variable. So, OLDPWD would now be – /home/techpiezo/Downloads
These two environment variables can be used to quickly switch between current working directory and previous working directory. For more information, follow the article we mentioned about in first paragraph.
Use echo shell builtin command to get the data stored in these two variables –
echo $PWD echo $OLDPWD
In conclusion, we have covered here two Bash environment variables here – PWD and OLDPWD.
Additional Info –
PWD environment variable shouldn’t be confused with pwd shell builtin command. Both of these are different. One is an environment variable while other is a shell builtin command. When we issue pwd in terminal it prints the name of current working directory in BASH.