Find line number of function call from sourcing file

前端 未结 3 1450
萌比男神i
萌比男神i 2020-12-17 14:50

I\'m trying to find a way to find out what file and line number a function was called from. The function is in a library file which is being sourced by my script.

3条回答
  •  孤街浪徒
    2020-12-17 14:53

    You are looking for caller it seems.

    $ 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
    

提交回复
热议问题