pwd

Print working directory.

Source: src/execution/builtins.f90

Synopsis

pwd [-LP]

Description

The pwd builtin prints the absolute pathname of the current working directory.

Options

OptionDescription
-LLogical path (with symlinks, default)
-PPhysical path (resolve symlinks)

Usage

Basic Usage

pwd
# /home/user/projects

Physical Path

cd /tmp
ln -s /var/log logs
cd logs

pwd -L
# /tmp/logs

pwd -P
# /var/log

Examples

In Scripts

#!/usr/bin/env fortsh
script_dir="$(pwd)"
echo "Running from: $script_dir"

Save and Restore

original_dir="$(pwd)"
cd /somewhere/else
# Do work...
cd "$original_dir"

Check Current Location

if [[ "$(pwd)" == "$HOME" ]]; then
    echo "You're in home directory"
fi

Variables

VariableDescription
$PWDCurrent directory (same as pwd -L)
$OLDPWDPrevious directory
echo $PWD       # Same as pwd
echo $OLDPWD    # Previous directory
cd -            # Goes to $OLDPWD

Implementation

The builtin returns shell%cwd which is updated on every directory change.

Exit Status

StatusCondition
0Success
1Error (rare)

vs /bin/pwd

Featurebuiltin/bin/pwd
SpeedFasterSlower
SymlinksUses $PWDMay differ
-P optionYesYes

Notes

  • pwd is faster than $(pwd) command substitution
  • $PWD is equivalent to pwd in most cases
  • Physical path may differ after following symlinks

See Also