As part of an error handling function, I need to report the currently executing command when an error is trapped. The bash $BASH_COMMAND string contains the command, but with nested non-expanded variables. I would like to avoid using eval if there is a better way.
Given these input variables:
path1="a/a/a"
path2="b/b/b"
BASH_COMMAND='mycommand "$path1" $path2'
Produce this output variable:
expanded='mycommand a/a/a b/b/b"
This works with eval, but I am concerned this may be unsafe:
expanded=$(eval echo "$BASH_COMMAND")
EDIT: as chepner explained in the comment below, this question is not answered by Bash expand variable in a variable because the string must be parsed and may contain multiple nested variables.