Search code examples
shposix

How to know the function name that is running on a POSIX shell script


I'm trying to figure out if there's a standard way (not a hack that works) to know the name of the function that is being executed currently on a POSIX shell script. I know I can return the name of the script with $0. How to apply something similar to functions?


Solution

  • A quick search of the latest POSIX standard indicates no, but:

    • The line number $LINENO is a POSIX feature.
    • You can't get the script name reliably (not even from $0), because it's not read only and it's not set by some shells.
    • You can trace the script using set -o xtrace.