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,
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,
This is what I did:
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 $_
Notice the last line there? Use $_. At least in Korn. YMMV in bash, csh, et al..
How the script was called is stored in the variable $0. You can use
readlink
to get the absolute file name:I upgraded the Edward Staudt's answer, to be able to deal with absolute-path symbolic links, and with chains of links too.
Ugly, but works... After run this, the path is $CPATH and the file is $CFILE
Using $_ provides the last command.
Works if I issue the command twice:
If I use a different sequence of commands:
The $_ variable returns "who"
You could use: