Is it possible for a shell script to know the directory the user is in when they run it? I am trying to develop a script like this which runs through a list of subdirectories and runs some git commands on them. One way might be to pass pwd to it, but there are some additional parameters it will require which is why I want to use a built-in function to get the working directory.

The example assumes the current directory.


for d in */ ; do
  echo ""
  echo "===================="
  echo ""
  echo "$d"

  git -C "$d" remote show origin
  echo ""
  echo "===================="
  echo ""
  • 1
    What's wrong with using pwd in the bash-script? – lw1.at Oct 16 '16 at 13:35
  • I don't do a lot of shell scripting and even with scripts I write I have to read them again to understand the constructs I use, so the less lines I write the better it is for me. – vfclists Oct 16 '16 at 13:51
  • 1
    see also: unix.stackexchange.com/questions/79571/… – Sundeep Oct 16 '16 at 16:03

POSIX shells (like bash) and fish make the path of the current working directory (a process property inherited by children and preserved across execution of commands) in the $PWD special variable in POSIX shells. The equivalent for csh-like shells is $cwd.

The variable is dynamic. Its content may change after you call a command that changes the current directory like cd (or pushd/popd in some shells). POSIX shells also maintain a $OLDPWD variable that contains the previous working directory from before you called a successful cd/pushd/popd.

The pwd command prints one path to the current directory. In POSIX shells, that command is often built-in and just prints the content of $PWD.

If you want to know what the working directory at the time the script started, you'll need to record it at the start of your script like:

#! /bin/sh -
cd ...

To know the current working directory of your parent process, on GNU/Linux, you can do

parent_PWD=$(readlink -ve "/proc/$PPID/cwd")

though you may not always have permission to get that information. If your parent died, you'll be adopted by init or by the child sub-reaper if any. However $PPID will not be updated, so you may get the cwd of the wrong process in that case (if the the pid of your parent has been reused).

| improve this answer | |

Sure, the script inherits the current directory of the shell/program that runs it. So all file name references would be relative to that, unless the script does a cd itself. Knowing the location of the script file itself is harder to do, and may be nigh impossible.

| improve this answer | |
  • Is there a built in variable that refers to the directory the user started in, in case the script itself executes additonal cd commands? PS. The location of the script itself is a solved problem - stackoverflow.com/questions/4774054/… – vfclists Oct 16 '16 at 14:03
  • Knowing the location of the script file itself is harder to do, and may be nigh impossible: SCRIPT_DIR="$(cd \"$(dirname \"$0\")\" && pwd)" – aff Nov 22 '18 at 16:22
  • 1
    @aff, try that when the script is started with say (exec -a hello /path/to/script.sh). The thing is that $0 can contain anything the process calling execve() wants it to contain. – ilkkachu Nov 22 '18 at 21:37

I don't think there is an environment variable for this, but you can make your own variable to use pretty easily without many commands.

For example:

starting_dir=$(pwd)  #use pwd to get the starting directory

#do things
cd some_example_dir/
#do more things

cd $starting_dir  #go back to the directory

In this case, you're just saving the starting directory's path as a string in the variable $starting_dir. It's saved as an absolute path, so you can cd to it any time.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.