Does the command
pwd in a shell script output the directory the shell script is in?
There are three independent "directories" at play here:
To demonstrate that they are independent, you can write a shell script, saved to /tmp/pwd.sh, containing:
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
Present (or Current) Working Directory
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.
If you want the directory of the current script, use the
Quick Test of
There is this concept called
Or better worded: the kernel keeps an idea of the
That could be read with (for a system with /proc):
And for the running shell (of which its PID should be $$):
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
Each time a shell, script or process execute a
A shell could change its
An script could tell the shell under which it is running to change the
Or some other
In all cases, the kernel
It returns directory from where it is called/run from & not where the script is !
Here is try.sh :
This output will make more clear :