Sample Header Ad - 728x90

In Bash 5.2+, is there a means to read the command currently being evaluated?

0 votes
1 answer
93 views
...that is to say, is there a means by which I can read the full command that's being evaluated AS it's happening? Here's a contrived example, but one I feel makes the question clear:
function showOwnEvaluation {
    local evaluatingCommand=????
    [[ "$evaluatingCommand" =~ ^showOwnEvaluation ]] && echo "Original Function"
}

alias sOE='showOwnEvaluation'

$ showOwnEvaluation 123
# Output: Original Function

$ sOE 123
# Output:
Basically the ability to run echo "$(!-1)" for the current, active command? A echo "$(!0)", if you will? What I'd like to accomplish here is this: if I'm assigning the result of an expansion to a variable:
someVar=$(myFunc)
...I'd like to be able to alter the response from myFunc *based on the name of the variable it's being set to*. I simply cannot figure out how to get the someVar= portion of the expression, if, indeed, it's even possible to DO such a thing (without slapping a full emulation over the terminal and monitoring all of the I/O, of course). I know this may seem like a nonsensical desire, here, but I actually do have a valid use-case. I'm just uncertain if it's even feasible.
Asked by NerdyDeeds (123 rep)
Jan 26, 2025, 12:53 AM
Last activity: Jan 26, 2025, 03:49 PM