[NTLUG:Discuss] Easy (for someone besides me) shell script question

Victor Brilon victor at victorland.com
Tue Dec 28 20:36:00 CST 2004


David Ross wrote:
> How do i get the working path of a script?

Do you mean the directory where the path resides or where the user who 
called it is?

> Or how does the script know where it is?
> #!/bin/sh
> echo `pwd`

$0 will return the name of the script depending on how it was invoked. 
I.E. if your script is called foo.sh and looks like this:
#!/bin/bash
echo $0

And you invoke it as /usr/bin/foo.sh, the it'll print /usr/bin/foo.sh. 
But if you cd into /usr/bin and invoke it as foo.sh, you'll just get 
foo.sh back.

So $0 probably isn't what you're looking for. And `pwd` isn't the right 
thing either since it gives you the pwd of the calling process, not of 
the script itself.

Victor



More information about the Discuss mailing list