4

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.

11
  • 2
    That question assumes you have just the name in the variable, not a string with one or more parameter expansions embedded in it. For this problem, you'd need to do some additional non-trivial parsing to isolate the parameter names first.
    – chepner
    Dec 7, 2015 at 22:23
  • 1
    Very, very nontrivial. I've seen POSIX sh parsers available (albeit none of them written in shell), but I've never seen a fully complete parser for bash syntax available in bash. Dec 8, 2015 at 18:30
  • 1
    Frankly, it's a lot more feasible to just dump all variables defined and let the person doing the debugging sort out which ones are pertinent. Dec 8, 2015 at 18:30
  • 1
    ...and the easy approaches will execute$(command-substitutions) and the like, which are inherently side-effecting operations. Dec 8, 2015 at 18:33
  • 1
    @KarolyHorvath, one can do better than parsing stderr -- with bash 4 or later, you can redirect set -x output to an arbitrary file descriptor with BASH_XTRACEFD, so the content can be directed to an arbitrary location or process. Dec 8, 2015 at 19:52

1 Answer 1

0

You can create a new file with the command and source that file then remove it. Here is a working script:

path1="a/a/a"
path2="b/b/b"

mycommand() {
  echo $@
}

MY_COMMAND='mycommand "$path1" $path2'

echo "$MY_COMMAND" > ./temp.sh
source ./temp.sh
rm ./temp.sh

BTW, I renamed BASH_COMMAND to MY_COMMAND because it is an internal environment variable containing the currently executed command with its arguments. Instead of writing to temp.sh, you can also generate a file using RANDOM inside the /tmp/ directory:

path1="a/a/a"
path2="b/b/b"

mycommand() {
  echo $@
}

MY_COMMAND='mycommand "$path1" $path2'
RAND_NUM=$RANDOM

echo "$MY_COMMAND" > /tmp/temp-$RAND_NUM.sh
source /tmp/temp-$RAND_NUM.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.