Link to home
Start Free TrialLog in
Avatar of parlays
parlays

asked on

BASH script, get the script directory

Hello!

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)
PWD

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.

thanks!
Avatar of arnold
arnold
Flag of United States of America image

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.
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`

OR

basedir which scriptname`

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

basedir `which scriptname`
Avatar of parlays
parlays

ASKER

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?
Avatar of parlays

ASKER

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:

thanks!
they are the same
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of parlays

ASKER

thanks!