Operating System - HP-UX
1752679 Members
5273 Online
108789 Solutions
New Discussion юеВ

Finding absolute path of 'sourced' shell script

 
vinay naik
Occasional Contributor

Finding absolute path of 'sourced' shell script

We use a ksh script which is sourced from different directories. This script sets project paths and other environment variables based on the absolute path of the script.
The issue is that the path of this script is not fixed as it changes based on the directory it is checked out.
If it is sourced from a different directory the $PWD or `dirname` displayed is that of the directory which sources it and 'not' the directory of the 'sourced script'.

Can someone please suggest a solution for this ?

6 REPLIES 6
Berd
Trusted Contributor

Re: Finding absolute path of 'sourced' shell script

Vinay,

You could use the 'which' command prior to executing. This would tell you which, pardon the pun, sourced script you are executing.

HTH

Regards,
Berd
Dennis Handly
Acclaimed Contributor

Re: Finding absolute path of 'sourced' shell script

Since a sourced file isn't a script, I don't think there is any way to get the path except have the caller set a variable.

Sourced files do undergo $PATH lookup, not sure if that is helpful.
Hakki Aydin Ucar
Honored Contributor

Re: Finding absolute path of 'sourced' shell script

to put a which OR find to source every time script run will have negative effect as a big delay.

why do not you consider a copy of source to known directory on your local ?
Bill Hassell
Honored Contributor

Re: Finding absolute path of 'sourced' shell script

Actually, "which" and "whereis" are deprecated because they do not match what the shell will do. The correct (and only useful) command is whence and whence -v. whence -v is so useful that it is always aliased to the "type" command. Things like $PWD are useless because users can change the contents and you never want to use a relative pathname (a sourced script always starts with: /)

To see why which and whereis are not useful:

which for
whereis for
whence -v for

The only correct answer is whence -v for. This is even more telling:

which type
whereis type
whence -v type

which and whereis both report that /usr/bin/type is the location of the command but your script will *not* run /usr/bin/type. Instead, it will run the alias. This is critically important for sourced scripts and other commands that a user may have aliased or have $PATH set to unsafe locations.

I will explictly set PATH and unset all aliases in my critical scripts to prevent accidents (or hacks):

export PATH=/usr/bin:/usr/sbin:/usr/contrib/bin
unalias -a



Bill Hassell, sysadmin
Dennis Handly
Acclaimed Contributor

Re: Finding absolute path of 'sourced' shell script

>Bill: "which" and "whereis" are deprecated because they do not match what the shell will do.

I always think of them as scummy C shell commands, not to be used for a real shell, unless you have a bad path setting.
Berd
Trusted Contributor

Re: Finding absolute path of 'sourced' shell script

Bill / Dennis - I will never again use which, whence is the way !

Berd