Get the name of the caller script in bash script

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

提交回复
热议问题