Get the name of the caller script in bash script

前端 未结 9 1822
长情又很酷
长情又很酷 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:08

    If you have /proc:

    $(cat /proc/$PPID/comm)
    
    0 讨论(0)
  • 2020-12-05 00:16

    Based on @user3100381's answer, here's a much simpler command to get the same thing which I believe should be fairly portable:

    PARENT_COMMAND=$(ps -o comm= $PPID)
    

    Replace comm= with args= to get the full command line (command + arguments). The = alone is used to suppress the headers.

    See: http://pubs.opengroup.org/onlinepubs/009604499/utilities/ps.html

    0 讨论(0)
  • 2020-12-05 00:19

    You can simply use the command below to avoid calling cut/awk/sed:

    ps --no-headers -o command $PPID
    

    If you only want the parent and none of the subsequent processes, you can use:

    ps --no-headers -o command $PPID | cut -d' ' -f1
    
    0 讨论(0)
提交回复
热议问题