LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How can I determine the path of a shell script that I'm running right now ? (https://www.linuxquestions.org/questions/linux-general-1/how-can-i-determine-the-path-of-a-shell-script-that-im-running-right-now-495401/)

basher400 10-25-2006 07:37 AM

How can I determine the path of a shell script that I'm running right now ?
 
ok, I'm in:
/usr1/boor/home/

and I'm running:
/usr1/Asher/home/scripts/main.sh

I need for main.sh to know the path it is running in - no matter from where I'm running it.

the $0 is the closest I got so far, the only problem is $0 also contains the name of the file itself ("main.sh") and I only want the path ("/usr1/Asher/home/scripts/")

any ideas ?
oh and I would rather not use the "cd" command in the script because it is needed to be in the same place later in the script.
thanks

druuna 10-25-2006 07:52 AM

Hi,

It's not clear to me what you actually want to do, but the following hopefully helps:

dirname $0 => return the directory part.

battletroll 10-25-2006 07:54 AM

Try this in your script
 
Quote:

Originally Posted by basher400
ok, I'm in:
/usr1/boor/home/

and I'm running:
/usr1/Asher/home/scripts/main.sh

I need for main.sh to know the path it is running in - no matter from where I'm running it.

the $0 is the closest I got so far, the only problem is $0 also contains the name of the file itself ("main.sh") and I only want the path ("/usr1/Asher/home/scripts/")

any ideas ?
oh and I would rather not use the "cd" command in the script because it is needed to be in the same place later in the script.
thanks


$PWD=`pwd`

SCerovec 10-25-2006 08:10 AM

did you read bash's man?
did you try
echo $_
?

basher400 10-25-2006 08:22 AM

Quote:

Originally Posted by SCerovec
did you read bash's man?
did you try
echo $_
?

echo $_ didn't work

basher400 10-25-2006 08:23 AM

Quote:

Originally Posted by battletroll
$PWD=`pwd`

this I tried.
it gives me the path of which i'm running script from (/usr1/boor/home/), not the path of main.sh

pwc101 10-25-2006 08:25 AM

Quote:

Originally Posted by battletroll
$PWD=`pwd`

$PWD is a system variable on my machine, so there's no need to define it. Just calling $PWD in the script will return the path.

basher400 10-25-2006 08:31 AM

Quote:

Originally Posted by druuna
Hi,

It's not clear to me what you actually want to do, but the following hopefully helps:

dirname $0 => return the directory part.

thanks

this works:
Code:

$(dirname $0)


All times are GMT -5. The time now is 07:28 PM.