Get the name of the caller script in bash script

前端 未结 9 1834
长情又很酷
长情又很酷 2020-12-04 23:58

Let\'s assume I have 3 shell scripts:

script_1.sh

#!/bin/bash
./script_3.sh

script_2.sh



        
9条回答
  •  粉色の甜心
    2020-12-05 00:06

    In case you are sourceing instead of calling/executing the script there is no new process forked and thus the solutions with ps won't work reliably.

    Use bash built-in caller in that case.

    $ cat h.sh 
    #! /bin/bash 
    function warn_me() { 
        echo "$@" 
        caller 
    } 
    $ cat g.sh 
    #!/bin/bash 
    source h.sh 
    warn_me "Error: You didn't do something" 
    $ . g.sh 
    Error: You didn't do something 3 
    g.sh
    $
    

    Source

提交回复
热议问题