You should be able to use BASH_LINENO:
$ cat test.sh
my_environment() {
echo "Stack size: ${#BASH_LINENO[@]}"
echo "Caller line: ${BASH_LINENO[$((${#BASH_LINENO[@]} - 2))]}"
}
my_environment
$ sh test.sh
Stack size: 2
Caller line: 5
That is, the second to last entry will be the line number where the call to my_environment
happened.
From the man bash
section about BASH_LINENO
:
An array variable whose members are the line numbers in source files where each corresponding member of FUNCNAME was invoked. ${BASH_LINENO[$i]} is the line number in the source file (${BASH_SOURCE[$i+1]}) where ${FUNCNAME[$i]} was called (or ${BASH_LINENO[$i-1]} if referenced within another shell function). Use LINENO to obtain the current line number.