[lug] Q: retrieving script name from within script alternative

Ron Wright halsaves at gmail.com
Tue Jul 5 15:45:07 MDT 2005


Bash gives you $BASH_SOURCE. For ksh and other POSIX shells maybe $_
fits your needs.  It captures the "last argument".  In the case of
sourcing a file, that should be your script as referenced by the user.

Ron Wright

On 7/2/05, Chan Kar Heng <karheng at softhome.net> wrote:
> hi there.
> 
> is there an alternative method for script name retrieval from within a
> script?
> 
> eg:
> i've a script called a.sh.
> from a.sh, i use $0 with basename to get the script name.
> i can use cd `dirname $0` followed by DIR=`pwd` to retrieve directory of
> the script.
> this doesn't work however, if i source the script like this:
> 
> . ./a.sh
> 
> $0 is assigned -, which means it's the login shell.
> 
> dug around over the net, but the answer seems elusive
> (can't figure out any better keywords to search with).
> 
> any guru might have any ideas?
> 
> the script i have there is actually a script containing settings,
> & i need it to initialize environment variables to the folder it is in,
> etc. sorta like saying JAVA_HOME=`dirname $0`...
> 
> btw, for people here who dual boot windoz & linux, try out colinux.
> i've just tried it out & it seems pretty good so far.
> allows linux to run from within windows, but in different way from
> machine virtualisation.
> 
> 
> thanks & rgds,
> 
> kh
> 
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug
>



More information about the LUG mailing list