What does pwd output?

Does the command pwd in a shell script output the directory the shell script is in?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There are three independent “directories” at play here:

  1. your current shell’s current working directory,
  2. the shell script’s current working directory, and
  3. the directory containing the shell script.

To demonstrate that they are independent, you can write a shell script, saved to /tmp/pwd.sh, containing:

#!/bin/sh
pwd
cd /var 
pwd

You can then change your pwd (#1 above) to /:

cd /

and execute the script:

/tmp/pwd.sh

which starts off by demonstrating your existing pwd (#1), then changes it to /var and shows it again (#2). Neither of those pwd‘s were “/tmp”, the directory that contains /tmp/pwd.sh (#3).

Method 2

Present (or Current) Working Directory

Does the command pwd in a shell script return the directory the shell script is in?

No.

Firstly, by definition, no shell script or shell command returns anything other than a numeric exit status between 0 – 255. That’s axiomatic, but not generally not what people mean when they ask these types of questions.

Secondly, pwd is both a Bourne shell builtin and a standard system binary. Either one prints the logical or physical current working directory, which is generally:

  1. Your location in the directory structure when you call a script or binary.
  2. Your current location after changing the working directory with cd or other utilities and builtins that modify the current working directory such as pushd or popd.

If you want the directory of the current script, use the dirname utility as described in the final section below.

Quick Test of pwd

As a quick test to see what pwd really prints, you can run the following:

# Create a shell script containing pwd.
cat <<-EOF > /tmp/test_pwd.sh
#!/bin/sh

pwd
EOF

# Make the script executable.
chmod 755 /tmp/test_pwd.sh

# Go somewhere on the filesystem, and call the test script.
cd /etc
/tmp/test_pwd.sh

This will print /etc, not /tmp, because your current working directory is currently /etc. This is the expected behavior.

Getting the Directory Containing a Script

You’re probably asking this question because you want to find the directory of the current script. In the general case, the following is the quick-and-dirty solution:

#!/usr/bin/env bash
echo $(dirname "$0")

This works because $0 generally contains the pathname used to invoke the script being executed, and the shell expansion uses the dirname utility to return the path excluding the filename portion. You can do something similar, but less portably, with the Bash parameter expansion "${0%/*}".

This is all a vast over-simplification, of course. Please read the Bash manual (especially the sections on positional parameters, special parameters, and BASH_SOURCE) and the man pages for readlink and realpath to get a fuller understanding of what the edge cases are, of which there are several.

However, in day-to-day scripting, the directory component of $0 is sufficient to tell you what you want to know. If you are doing something complicated enough where $0 doesn’t hold the information you actually need, and you require more complicated constructs like:

echo $(dirname "$(realpath "$0")")

then you’re probably making your life more difficult than it needs to be.

Method 3

pwd returns 0 unless its current working directory cannot be opened.

mkdir /tmp/d; cd "$_"
pwd && pwd -P; echo "$?"
rmdir ../d
pwd && pwd -P; echo "$?"

/tmp/d
/tmp/d
0
/tmp/d
pwd: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
1

Method 4

There is this concept called cwd that every running process keeps track of.

Or better worded: the kernel keeps an idea of the cwd of each process.

That could be read with (for a system with /proc):

readlink /proc/$PID_of_PROCESS/cwd

And for the running shell (of which its PID should be $$):

$ readlink /proc/$$/cwd

The shell keeps track of the same information (even if sometimes they might get out of sync) in the variable $PWD, and in the command pwd:

$ cd /tmp; echo "Present working directory: $PWD"
Present working directory: /tmp
$ cd /tmp; echo "Current working directory: $(pwd)"
Current working directory: /tmp

So, the pwd command presents the cwd of each process:

The place each process stands inside the directory tree.

Each time a shell, script or process execute a cd, all those variables get updated (except some corner cases).

A shell could change its pwd by executing a cd /tmp for example.

An script could tell the shell under which it is running to change the pwd with a cd /tmp as well.

Or some other c process might call the kernel to execute an equivalent of a cd /tmp.

In all cases, the kernel cwd and (if the process is a shell) the shell’s pwd gets updated.

Method 5

It returns directory from where it is called/run from & not where the script is !

Here is try.sh :

echo "The current working directory: $PWD"

This output will make more clear :

[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90f1fbf1e2e0f5d0d1d4ddbdc0c2dfd4bddfddded9">[email protected]</a> ~]$ sh try.sh
The current working directory: /mnt/home/akarpe
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b3b9b3a0a2b79293969fff82809d96ff9d9f9c9b">[email protected]</a> ~]$ sh try/try.sh
The current working directory: /mnt/home/akarpe
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84e5efe5f6f4e1c4c5c0c9a9d4d6cbc0a9cbc9cacd">[email protected]</a> ~]$ sh ./try.sh
The current working directory: /mnt/home/akarpe
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d5c565c4f4d587d7c7970106d6f72791072707374">[email protected]</a> ~]$ sh ./try/try.sh
The current working directory: /mnt/home/akarpe
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfded4decdcfdafffefbf292efedf0fb92f0f2f1f6">[email protected]</a> ~]$ cd try
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92f3f9f3e0e2f7d2d3d6dfbfc2c0ddd6bfdddfdcdb">[email protected]</a> try]$ sh ./try.sh
The current working directory: /mnt/home/akarpe/try
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="422329233032270203060f6f12100d066f0d0f0c0b">[email protected]</a> try]$ sh ../try.sh
The current working directory: /mnt/home/akarpe/try


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x