61

Let's assume I have 3 shell scripts:

script_1.sh

#!/bin/bash
./script_3.sh

script_2.sh

#!/bin/bash
./script_3.sh

the problem is that in script_3.sh I want to know the name of the caller script.

so that I can respond differently to each caller I support

please don't assume I'm asking about $0 cause $0 will echo script_3 every time no matter who is the caller

here is an example input with expected output

  • ./script_1.sh should echo script_1

  • ./script_2.sh should echo script_2

  • ./script_3.sh should echo user_name or root or anything to distinguish between the 3 cases?

Is that possible? and if possible, how can it be done?

this is going to be added to a rm modified script... so when I call rm it do something and when git or any other CLI tool use rm it is not affected by the modification

1
  • 12
    Beware modifying the rm command (by giving using cover scripts called rm, or aliases, or functions). You'll come to rely on the functionality, and then one day you'll find yourself using the raw rm command without the protections, and you'll do serious damage because you've been lulled into a false sense of security. Dec 13, 2013 at 18:27

10 Answers 10

86

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

3
  • 10
    Cygwin's ps does not support the -o option.
    – Koraktor
    Sep 2, 2015 at 11:34
  • 2
    If the parent-name is longer than 15 chars, it gets truncated to 15 chars.... (tested on RHEL8).
    – Rop
    Aug 27, 2022 at 15:30
  • 1
    @Rop This is a very standard limitation, the comm field is 15 char and generally just the executable name. On multi-threaded processes, it's also common for threads to change their comm to help differentiate between threads. The full command being invoked is usually the first argument of args, though using ps it could be difficult to separate names/paths that could contain a space form the rest of the arguments (on Linux, reading the null-terminated /proc/self/cmdline would be a safe way to get the full command and its arguments). Sep 16, 2022 at 2:46
24

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 -n server.sh 
  1  #!/bin/bash
  2  
  3  source common.sh
  4  
  5  [[ $# -ne 2 ]] && warn_me "Error: You messed up"
  6
$ cat -n common.sh 
  1  #!/bin/bash
  2  
  3  function warn_me() {
  4      echo `caller | awk '{print $2":"$1}'` says: "$@"
  5  }
  6
$ ./server.sh 
./server.sh:5 says: Error: You messed up
$ 

Source

2
  • @JerryGreen it's a bash built-in. What shell are you using? Available at least on GNU bash.
    – Kashyap
    Nov 7, 2020 at 13:05
  • I use standard terminal which comes with macos Catalina (10.15.7), it comes with zsh/bash/sh Nov 7, 2020 at 17:01
21

The $PPID variable holds the parent process ID. So you could parse the output from ps to get the command.

#!/bin/bash
PARENT_COMMAND=$(ps $PPID | tail -n 1 | awk "{print \$5}")
8
  • Inventive, and probably about as good as you're going to get. Not particularly nice, though. Dec 13, 2013 at 18:26
  • On some platforms you just need the right options to ps, so the tail and Awk (which could easily be refactored to just Awk, btw) can be avoided.
    – tripleee
    Dec 13, 2013 at 18:32
  • 3
    this prints /bin/bash
    – a14m
    Dec 13, 2013 at 18:33
  • 2
    @artmees Try it with awk "{print \$6}" Dec 13, 2013 at 19:03
  • using $6 make ./script_3 prints '\n' empty string and ./script_2 return '\n./script_3'
    – a14m
    Dec 13, 2013 at 19:31
6

Based on @J.L.answer, with more in depth explanations, that works for :

cat /proc/$PPID/comm

gives you the name of the command of the parent

If you prefer the command with all options, then :

cat /proc/$PPID/cmdline

explanations :

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

Check man proc

3
  • this doesn't run on MacOs
    – a14m
    Mar 6, 2018 at 9:12
  • on linux this is the best answer Jan 18 at 13:29
  • It's worth mentioning that cat /proc/<PID>/comm truncates the program's name to the first 15 characters. A least, that's the result I'm getting on the various Linux hosts I'm using. To obtain a program name that exceeds 15 characters, I use cat /proc/<PID>/cmdline with additional steps to (a) strip the leading directory components from the program path string (e.g., basename(1)) and (b) strip command line arguments that follow the program name. Jul 26 at 17:27
2

Couple of useful files things kept in /proc/$PPID here

  • /proc/*some_process_id*/exe A symlink to the last executed command under *some_process_id*
  • /proc/*some_process_id*/cmdline A file containing the last executed command under *some_process_id* and null-byte separated arguments

So a slight simplification.

sed 's/\x0/ /g' "/proc/$PPID/cmdline"
7
  • this is not working sed: /proc/97727/cmdline: No such file or directory
    – a14m
    Dec 13, 2013 at 19:34
  • @artmees What files do you have if you cd to /proc/97727? And if you don't have that, do you have any pid directories in /proc? Dec 13, 2013 at 19:40
  • i don't have /proc directory
    – a14m
    Dec 13, 2013 at 20:20
  • @artmees What platform are you running on? Dec 13, 2013 at 20:25
  • mac osx but it needs to run on both osx and linux
    – a14m
    Dec 13, 2013 at 20:31
1

If you have /proc:

$(cat /proc/$PPID/comm)
2
  • Can you explain a bit more? Jan 5, 2017 at 15:09
  • This is a UUOC. You can do a simple $(< /proc/$PPID/comm)
    – lovasoa
    May 6, 2019 at 15:04
0

Declare this:

PARENT_NAME=`ps -ocomm --no-header $PPID`

Thus you'll get a nice variable $PARENT_NAME that holds the parent's name.

0

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

As others have mentioned, the output of ps -o comm= $PPID is truncated to 15 characters. In another question the answer was

cut -d $'\0' -f 2 /proc/$PPID/cmdline

Obviously you must have /proc on your system. Note that the output of ps -o args= $PPID does not have 0x00 as separator.

You can then use, e.g. basename or readlink -f, to maybe add more sense to the calling filename.

-4

You could pass in a variable to script_3.sh to determine how to respond...

script_1.sh

#!/bin/bash
./script_3.sh script1

script_2.sh

#!/bin/bash
./script_3.sh script2

script_3.sh

#!/bin/bash
if [ $1 == 'script1' ] ; then
  echo "we were called from script1!"
elsif [ $1 == 'script2' ] ; then
  echo "we were called from script2!"
fi
3
  • this is not possible as i'm trying add a script that handles special case of rm and i want it to be called if i called rm not if git used rm to remove some info.
    – a14m
    Dec 13, 2013 at 18:00
  • please clarify your original question then, because that was not clear from your examples.
    – Donovan
    Dec 13, 2013 at 18:01
  • I do not think it is possible to obtain the calling program from within a bash script the way you want, I'm sorry.
    – Donovan
    Dec 13, 2013 at 18:02

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.