What is current directory of shell script? Is this current directory from which I called it? Or this directory where the script is located?
-
See also [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory) which is superficially about Python but also relevant here. – tripleee Apr 13 '22 at 06:29
7 Answers
As already mentioned, the location will be where the script was called from. If you wish to have the script reference it's installed location, it's quite simple. Below is a snippet that will print the PWD and the installed directory:
#!/bin/bash
echo "Script executed from: ${PWD}"
BASEDIR=$(dirname $0)
echo "Script location: ${BASEDIR}"

- 33,893
- 13
- 69
- 83

- 2,650
- 2
- 12
- 9
-
This doesn't work for me. It only gives the name of the containing dir, not the full path. For example, if I do that from a script in `/home/user/scripts/x.sh`, it returns `scripts`, not `/home/user/scripts`. – Synchro Feb 08 '22 at 10:08
-
2I think this is because your current working directory is `/home/user` and you called the script by `scripts/x.sh`. Hence, `$0` value inside the script is `scripts/x.sh`. In order to get the absolute path of the script, you can use `BASEDIR=$(cd $(dirname $0) && pwd)` – mnabil Mar 18 '22 at 11:12
Most answers get you the current path and are context sensitive. In order to run your script from any directory, use the below snippet.
DIR="$( cd "$( dirname "$0" )" && pwd )"
By switching directories in a subshell, we can then call pwd
and get the correct path of the script regardless of context.
You can then use $DIR
as "$DIR/path/to/file"

- 2,612
- 3
- 27
- 33
-
Can you explain what's going on? When my script gets run more than 20 times simultaneously, I get the following error: `....sh: fork: retry: Resource temporarily unavailable` The script gets run by OpenVPN. (under nogroup group and nobody account) – HosseyNJF Mar 25 '20 at 13:27
-
Another advantage of this example is that `readlink` and `realpath` doesn't work in Systemd unit files when they call a shell script. So this one is the most portable POSIX solution that I found so far. – Lanti Aug 22 '22 at 14:23
The current(initial) directory of shell script is the directory from which you have called the script.

- 1,317
- 1
- 16
- 35

- 373
- 1
- 4
You could do this yourself by checking the output from pwd
when running it.
This will print the directory you are currently in. Not the script.
If your script does not switch directories, it'll print the directory you ran it from.

- 18,829
- 16
- 59
- 101
Adding to this comment, I think most optimized and logic way is:
realpath `dirname $0`
or
realpath $(dirname $0)

- 31
- 2
-
How to [link to a comment](https://meta.stackexchange.com/a/120688/509881). – Y. E. Sep 18 '22 at 16:25
To print the current working Directory i.e. pwd just type command like:
echo "the PWD is : ${PWD}"

- 1,141
- 12
- 21

- 27
- 1
-
1Do you mean `${PWD}` ? Shell variable names are case sensitive and traditionally use upper case. `${pwd}` is not a predefined variable name unless you specifically set it to something. – Z4-tier Jul 07 '22 at 06:24
-
you could also do it completely using posix shell script
#!/bin/sh
get_current_directory() {
current_file="${PWD}/${0}"
echo "${current_file%/*}"
}
CWD=$(get_current_directory)
echo "$CWD"

- 761
- 4
- 7