Does the command
pwd in a shell script output the directory the shell script is in?
There are three independent "directories" at play here:
- your current shell's current working directory,
- the shell script's current working directory, and
- 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 /:
and execute the script:
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).
Present (or Current) Working Directory
Does the command pwd in a shell script return the directory the shell script is in?
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.
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:
- Your location in the directory structure when you call a script or binary.
- 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
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
/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
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
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.
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):
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
$ cd /tmp; echo "Present working directory: $PWD" Present working directory: /tmp $ cd /tmp; echo "Current working directory: $(pwd)" Current working directory: /tmp
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
In all cases, the kernel
cwd and (if the process is a shell) the shell's
pwd gets updated.
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 :
[akarpe@ADM-PROD-OMNI ~]$ sh try.sh The current working directory: /mnt/home/akarpe [akarpe@ADM-PROD-OMNI ~]$ sh try/try.sh The current working directory: /mnt/home/akarpe [akarpe@ADM-PROD-OMNI ~]$ sh ./try.sh The current working directory: /mnt/home/akarpe [akarpe@ADM-PROD-OMNI ~]$ sh ./try/try.sh The current working directory: /mnt/home/akarpe [akarpe@ADM-PROD-OMNI ~]$ cd try [akarpe@ADM-PROD-OMNI try]$ sh ./try.sh The current working directory: /mnt/home/akarpe/try [akarpe@ADM-PROD-OMNI try]$ sh ../try.sh The current working directory: /mnt/home/akarpe/try