Can I get the absolute path to the current script in KornShell?

44,313

Solution 1

You could use:

## __SCRIPTNAME - name of the script without the path
##
typeset -r __SCRIPTNAME="${0##*/}"

## __SCRIPTDIR - path of the script (as entered by the user!)
##
__SCRIPTDIR="${0%/*}"

## __REAL_SCRIPTDIR - path of the script (real path, maybe a link)
##
__REAL_SCRIPTDIR=$( cd -P -- "$(dirname -- "$(command -v -- "$0")")" && pwd -P )

Solution 2

In korn shell, all of these $0 solutions fail if you are sourcing in the script in question. The correct way to get what you want is to use $_

$ cat bar

echo dollar under is $_
echo dollar zero is $0

$ ./bar

dollar under is ./bar
dollar zero is ./bar

$ . ./bar
dollar under is bar
dollar zero is -ksh

Notice the last line there? Use $_. At least in Korn. YMMV in bash, csh, et al..

Solution 3

Well it took me a while but this one is so simple it screams.

_SCRIPTDIR=$(cd $(dirname $0);echo $PWD)

since the CD operates in the spawned shell with $() it doesn't affect the current script.

Solution 4

How the script was called is stored in the variable $0. You can use readlink to get the absolute file name:

readlink -f "$0"

Solution 5

The variable $RPATH contains the relative path to the real file or the real path for a real file.

CURPATH=$( cd -P -- "$(dirname -- "$(command -v -- "$0")")" && pwd -P )

CURLOC=$CURPATH/`basename $0`

if [ `ls -dl $CURLOC |grep -c "^l" 2>/dev/null` -ne 0 ];then

    ROFFSET=`ls -ld $CURLOC|cut -d ">" -f2 2>/dev/null`

    RPATH=`ls -ld $CURLOC/$ROFFSET 2>/dev/null`

else

    RPATH=$CURLOC

fi

echo $RPATH
Share:
44,313

Related videos on Youtube

brabster
Author by

brabster

I'm brabster on twitter. My qualifications: SCJP 5 (2008) SCWCD 5 (2009) MSc. Advance Computer Science (2013) I've been following the progress of stackoverflow with interest from the beginning, proud to have been involved with the beta test.

Updated on July 09, 2022

Comments

  • brabster
    brabster almost 2 years

    Is it possible to find out the full path to the script that is currently executing in KornShell (ksh)?

    i.e. if my script is in /opt/scripts/myscript.ksh, can I programmatically inside that script discover /opt/scripts/myscript.ksh ?

    Thanks,

  • brabster
    brabster about 15 years
    thanks for the $0 tip, what's readlink? doesn't seem to be on my system
  • brabster
    brabster about 15 years
    Hmmm need something totally within ksh, no dependencies. Thanks though
  • soulmerge
    soulmerge about 15 years
    I'm afraid that won't be possible - unless ksh has a possibility I am not aware of for interacting with the file system. What's your OS?
  • Henk Langeveld
    Henk Langeveld almost 12 years
    $0 refers to the name of the command as it was invoked, and does not need to refer to a file system object.
  • synthesizerpatel
    synthesizerpatel over 10 years
    Great answer - I was hoping this would also work if you source the script i.e. '. /some/script.sh' but it doesn't.
  • Mat M
    Mat M almost 10 years
    @synthesizerpatel: In ksh93, you have ${.sh.file} which also works for sourced files.
  • Tom Quarendon
    Tom Quarendon over 9 years
    As pointed out by Gruik, this doesn't work if there is a symbolic link to the shell script itself. So if you link the command into, say, /usr/bin, then __REAL_SCRIPTDIR will return /usr/bin, which is (most likely) not what you want. You do apparently need readlink for this
  • Tom Quarendon
    Tom Quarendon over 9 years
    This does appear to be the only method available that copes with the situation of a symlink to the script itself, horrible though it is.
  • andreee
    andreee over 7 years
    This answer should be incorporated into the accepted one. I was struggling for half an hour sourcing my ksh script until I realized that I need $_.