Is there a Bash variable containing the name of the previously executed function?
2
votes
4
answers
165
views
I want to line up a couple of Bash functions based on the success or failure of the execution of the preceding function.
If something fails, I want to print an error message:
function_x has failed
.
**Example 1**
function_x() {
false
}
if ! function_x ; then
echo "function_x has failed"
else
echo "call another function here"
fi
So the above works fine, but I would like to make the content of the error message more specific than above, maybe through the use of an environment variable carrying the name of the previously run function. Something like below:
**Example 2**
if ! function_x ; then
echo "${PREVIOUS_FUNCTION} has failed"
else
echo "call another function here"
fi
Now, my question is whether such a generic environment variable exists, or whether there is some other way to make that error message "function [function_name] has failed"
more generic than in Example 1.
Asked by vrms
(287 rep)
May 4, 2025, 04:33 AM
Last activity: May 5, 2025, 10:57 AM
Last activity: May 5, 2025, 10:57 AM