LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-25-2006, 07:37 AM   #1
basher400
Member
 
Registered: Mar 2005
Posts: 51

Rep: Reputation: 15
Red face 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

Last edited by basher400; 10-25-2006 at 07:39 AM.
 
Old 10-25-2006, 07:52 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

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

dirname $0 => return the directory part.
 
Old 10-25-2006, 07:54 AM   #3
battletroll
Member
 
Registered: Dec 2004
Location: Alabama
Distribution: Slackware, Solaris,Fedora, CentOS, Redhat, SGI
Posts: 63

Rep: Reputation: 16
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`
 
Old 10-25-2006, 08:10 AM   #4
SCerovec
Member
 
Registered: Oct 2006
Location: Cp6uja
Distribution: Slackware and Porteus
Posts: 648

Rep: Reputation: 45
did you read bash's man?
did you try
echo $_
?
 
Old 10-25-2006, 08:22 AM   #5
basher400
Member
 
Registered: Mar 2005
Posts: 51

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by SCerovec
did you read bash's man?
did you try
echo $_
?
echo $_ didn't work
 
Old 10-25-2006, 08:23 AM   #6
basher400
Member
 
Registered: Mar 2005
Posts: 51

Original Poster
Rep: Reputation: 15
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
 
Old 10-25-2006, 08:25 AM   #7
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,846

Rep: Reputation: 128Reputation: 128
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.
 
Old 10-25-2006, 08:31 AM   #8
basher400
Member
 
Registered: Mar 2005
Posts: 51

Original Poster
Rep: Reputation: 15
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a shell script edited, so all users can use it without having to type the whole path Goalswish Linux - Newbie 5 05-11-2006 10:37 AM
shell script to read ps -e output and determine process double processes. dr_zayus69 Programming 1 09-21-2005 05:37 PM
how to determine script path cxc Programming 3 09-10-2005 08:36 AM
How to determine whether a PROCESS is running, by a SHELL program? yuzuohong Linux - General 4 01-21-2003 08:41 PM
c/c++ running a shell script? thermalpants Programming 3 05-12-2002 11:41 AM


All times are GMT -5. The time now is 05:15 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration