Skip to main content

How to Get the Current Folder in the Terminal with pwd

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Every terminal works in a folder, and many commands need its full path: to set a variable to "this folder", to copy a path into a config file, or just to check where you are. pwd prints it, and $PWD puts it straight into another command. This guide shows both in zsh, bash and PowerShell.

Syntax​

In zsh and bash:

Syntax
pwd
echo "$PWD"
<command> "$PWD/<something>"
PartWhat it means
pwdPrints the full path of the current folder
$PWDA variable that holds the same path, for use inside other commands

The zsh manual describes PWD as "The present working directory. This is set when the shell initializes and whenever the directory changes."

In PowerShell:

Syntax
"$PWD"
Get-Location

Microsoft's documentation says $PWD "Contains a path object that represents the full path of the current directory location".

The Example​

You print the folder the terminal is in, then create a reports folder, move into it, and print the path again.

Step 1: Print the Current Folder​

Terminal
echo "$PWD"
pwd
mkdir -p reports
cd reports
echo "$PWD"
Output
/Users/you/source/shell-demo
/Users/you/source/shell-demo
/Users/you/source/shell-demo/reports

The zsh terminal after the five commands: the path /Users/you/source/shell-demo twice, then /Users/you/source/shell-demo/reports after cd reports.

pwd and $PWD gave the same path. After cd reports, $PWD changed with it. In PowerShell, "$PWD" gives the path as text, while Get-Location prints it as a small table with a Path column. On Windows, the paths are Windows paths.

Using It in Another Command​

Because $PWD is a variable, it can go anywhere a path can. A common use is pointing a tool at the folder you're in:

Terminal
export APP_HOME="$PWD"

That stores the current folder's full path in APP_HOME for every program you start from this terminal. Keep the double quotes, so a path with spaces stays one piece.

Common Mistakes​

MistakeWhat happensDo this instead
$pwd in lowercase in zsh or bashNames are case-sensitive there, so it's a different, unset variable$PWD
Leaving out the quotes: export APP_HOME=$PWDWorks until a folder name has a space"$PWD"

Troubleshooting​

SymptomLikely causeFix
The path isn't the folder you expectedThe terminal is somewhere elsecd to the right folder first, then use $PWD

FAQ​

What's the difference between pwd and $PWD?​

pwd is a command that prints the path. $PWD is a variable holding it, which you can use inside other commands without running anything.

Does $PWD follow cd?​

Yes, as Step 1 showed: it changed to .../reports right after cd reports.

Conclusion​

pwd and $PWD both gave /Users/you/source/shell-demo, and $PWD followed cd into reports. Use "$PWD" whenever a command needs the full path of the folder you're in.

Additional Resources​