tags:

views:

377

answers:

4

How may I detect the name of the directory (or better yet the entire path) in which my shell script is run?

+3  A: 

what shell? What operating system?

For starters try

man pwd
$PWD
aaa
seems to work thanks
goe
And if you do want just the directory's name, instead of the full path, read man basename too.
Roger Pate
+1  A: 

This is not as trivial as it looks like. Check out this question and this

Pekka
A: 

alternative method

pid=$$
path=$(ps -eo pid,args| awk -vp=$pid '$1~p{print $3}')
case "$path" in
    ./* ) pwd;;
    * ) echo $path;;
esac
A: 

This, I believe, is the most portable way:

dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
glenn jackman