Capturing script name including full path inside the script inself

25 pts.
Tags:
Linux
Linux administration
Red Hat Enterprise Linux
Scripting
Variable fields
I am trying to write a script to update $PATH. Instead of hard coding the paths into the script itself I want to dynamically do this by capturing the full file path of the script itself.

I.e /loc1/loc2/loc3/modifypath.sh.  instead of going  export PATH="$PATH:/loc1/loc2/loc3" I want to extract the full file path from the script call into a variable to add to $PATH.  That way no matter where the script is located it will modify $PATH with the location where it is. 



Software/Hardware used:
Linux Red Hat Enterprise 5.3
ASKED: October 1, 2010  3:25 PM
UPDATED: October 1, 2010  6:49 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

Try something like this:

<pre>current_path=$(pwd)
echo “Adding $current_path to PATH”
PATH=”$PATH:$current_path”</pre>

—————-

Discuss This Question: 3  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Jahill
    That would be ok if I were in the directory the script is in. I need to be able to run it from any location most likely root when I open a window and get the path to the directory the script is in.
    25 pointsBadges:
    report
  • carlosdl
    Ok, I understand now. How about this ?
    current_path=$(dirname $(readlink -f $0))
    65,110 pointsBadges:
    report
  • Jahill
    Perfect Thank you very much.
    25 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following