# Get job script directory with SGE

With the Sun Grid Engine, how can I get the directory where the job script is located, within the job script?

I need this so I can start the script with

cd script-location


to make sure it'll have access to all the necessary files. So far I have hard-coded the script directory into each script, which makes it much too easy to make mistakes.

• You can find out your current working directory using the PWD environment variable. Also, an absolute path for script-location should ensure you change to the right directory, regardless of your working directory when the script starts (you're already doing this). Is that what you're looking for? I typically code in paths using environment variables, if possible, to make hacking scripts easy. – Geoff Oxberry Apr 19 '13 at 16:06

The #\$ -cwd option to your job script will guarantee that SGE places you in the submission directory automatically so that you don't have to do cd script-location manually. The environment variable SGE_O_WORKDIR is defined and available to your job script as the working directory that you submitted it from as well.
• I found -cwd, but I misunderstood it as referring to the locations of the job.o* and job.po* files. SGE_O_WORKDIR sounds like it should definitely work. Of course everything is documented somewhere, but it's not always obvious where (google is usually better than a man page, but apparently not in this case) ... and experimenting with job scripts is both tedious and frustrating because of the waiting time. Thank you for the answer! – Szabolcs Apr 19 '13 at 16:42