Solved

Finding a bash script's full path

Posted on 2006-11-08
4
452 Views
Last Modified: 2008-02-01
I'm wondering if any experts in bash could offer me a better way to computer the full pathname to $0 in a script.

Specifically, I need to know the path the script is in, so it can do it's work in that (and only in that) directory.

Right now, I'm using the following four lines:
DIR="$PWD/`dirname $0`"      #Compute a full path name to this script
DIR=${DIR%.}            #Strip off trailing period
DIR=${DIR%\/}            #Strip off trailing slash
DIR=${DIR/\/\/\//\/}#Reduce any initial /// to /

The first line creates a full path, but it may have some strangeness (e.g., a trailing period if the bash script is launched directly from the directory, the odd trailing "/" in some cases, and the problem of "///home/xyz/abc", which $PWD can produce.

This four-line sequence works; I'm interesting in finding out of there any easier way to do this for my future bash scripts
0
Comment
Question by:CAOgdin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17902522
which $0
0
 

Author Comment

by:CAOgdin
ID: 17902559
Nice one.  I wonder how it missed that command.

Still not quite there, though.  I need it without the name of the script...just the path to the script itself.

It returns /home/xyz/abc/test for a script named "test."  I need just /home/xyz/abc
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 17902608
dirname `which $0`
0
 

Author Comment

by:CAOgdin
ID: 17902665
Slick!  I'm impressed (and it urges me on to learn more about bash).

Thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
string initialization in java 11 122
copyEndy  challenge 15 107
mapAB Challlenge 35 186
What language/protocol is the Angular Chat? 2 112
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question