Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


BASH script, get the script directory

Posted on 2012-09-02
Medium Priority
Last Modified: 2012-09-03

I am running a shell script from within another shell script.  They are in different directories and wanted to know the best way to get the current directory that the sub shell script is running from.

I've tried using:
$(dirname $0)

But those show the location of the original shell script.  Every command seems to act as if it is in the original shell script.

I am calling the second shell script from the first using the '.' dot command.  Any advice is appreciated.

Question by:parlays
LVL 81

Expert Comment

ID: 38359563
If you do not have cd or chdir within the scripts, the directory would reflect directory (cwd) from when the script was started.
Since the scripts seem to be running without an issue, it sounds as though explicit and full paths are being used.
LVL 40

Expert Comment

ID: 38359584
as mentioned by arnold the current working directory or the present working directory will not change unless you change it! If you want the location of the script called then it will be the current directory plus the relative path to the script or if the script is in the set PATH then the command

basedir `which command`


basedir which scriptname`

should give the dir name in which the command or script is in.
LVL 40

Expert Comment

ID: 38359586

basedir `which scriptname`
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.


Author Comment

ID: 38359667
Sweet!  Thanks guys so much for the information.  A quick follow up questions and I will award you guys points.  Is there a difference between cd and chdir or is one just an alias?

Author Comment

ID: 38360400
I've requested that this question be closed as follows:

Accepted answer: 300 points for arnold's comment #a38359563
Assisted answer: 200 points for omarfarid's comment #a38359584
Assisted answer: 0 points for parlays's comment #a38359667

for the following reason:

LVL 40

Expert Comment

ID: 38359760
they are the same
LVL 68

Accepted Solution

woolmilkporc earned 2000 total points
ID: 38360096
The problem with "$0" is due to the source ("dot") command by which the second script is called.

Scripts called this way become part of the calling script, just as if you had inserted the code contained in the called script into the calling script (as per "copy-and-paste", for example).

So it's no wonder that $0 still contains the name of the main script.

Using "dirname /path/to/scriptname" is indeed the only way how to get the path of the called script.

Attention: Using dirname (or basedir) $(which scriptname) successfully would require the path of "scriptname" being in your PATH variable, which does not necessarily have to be the case, obviously

Btw. using the "source" or "dot" method is only required if you're changing or setting  (environment) variables in the called script and want theses changes being made known to the calling script.


Author Closing Comment

ID: 38360401

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

580 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