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

前端 未结 12 739
粉色の甜心
粉色の甜心 2020-12-29 06:26

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

12条回答
  •  半阙折子戏
    2020-12-29 06:48

    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..

提交回复
热议问题