Get the name of the caller script in bash script

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

    Based on @J.L.answer, with more in depth explanations (the only one command that works for me (linux)) :

    cat /proc/$PPID/comm
    

    gives you the name of the command of the parent pid

    If you prefer the command with all options, then :

    cat /proc/$PPID/cmdline
    

    explanations :

    • $PPID is defined by the shell, it's the pid of the parent processes
    • in /proc/, you have some dirs with the pid of each process (linux). Then, if you cat /proc/$PPID/comm, you echo the command name of the PID

    Check man proc

提交回复
热议问题