pwd command, and you probably know, reports the current working directory in your Unix shell. PWD stands for Present Working Directory. In addition to pwd, there’s also a special variable – one of the user environment variables – called PWD, that you can also use.
Just to remind you, pwd command is a super simple way of confirming where you are in the filesystem tree.
Usually, your shell session starts in your home directory. For me and my username greys, this means /home/greys in most distributions:
If I then use cd command to visit some other directory, pwd command will help me confirm it:
Most Unix shells have PWD as a variable. It reports the same information as pwd command but saves children processes the trouble of using pwd command and getpwd system call just to confirm the working directory of their parent process.
So, you can just do this to confirm $PWD value:
… which really helps in shell scripting, cause you can do something like this:
When we run this, the script will compare standard $HOME variable (your user’s homedir) to the $PWD variable and will behave differently if they match.
I’ve created and saved pwd.sh in my projects directory for bash scripts: /home/greys/proj/bash:
If I now change back to my home directory:
… the script will thank me for it:
Have fun using pwd command and $PWD variable in your work and shell scripting!