Johannes
Johannes

Reputation: 3099

How can a script know the directory it resides in?

I work on a Solaris 8 machine and have a script which is being sourced. The script wants to know in which path itself is located. I tried multiple solutions from e.g. Unix shell script find out which directory the script file resides? , but all of them are based on $0 or pwd. I tried this so far:

$ echo $OS
SunOS
$ echo $OSTYPE
solaris
$ bash --version
GNU bash, version 2.02.1(1)-release (sparc-sun-solaris2.5.1)
Copyright 1998 Free Software Foundation, Inc.
$ pwd
/home/username
$ cat source-me/source-me 
pwd
echo $PWD
echo $0
echo ${BASH_SOURCE[0]}
$ . source-me/source-me 
/home/username
/home/username
-bash

$

Is the task solveable at all?

Upvotes: 2

Views: 87

Answers (1)

jhnc
jhnc

Reputation: 16819

You can set the location when you install the script.

$ pwd
/home/username
$ cat source-me/source-me
IAmHere=/home/username/source-me/source-me
echo "$IAmHere"
$ . source-me/source-me
/home/username/source-me/source-me
$

Upvotes: 0

Related Questions