When I set a DEBUG trap in Bash like this:
set -o functrace
trap 'echo "# $BASH_COMMAND" >&2' DEBUG
Suddenly, this function stopped working:
getBase() {
local base="$1"
base=$(basename "${base%%\?*}")
: "${base//+/ }"; printf -v base '%b' "${_//%/\\x}"
base=$(echo "$base" | sed -e 's/[^A-Za-z0-9._-]/_/g')
echo "$base"
return 0
}
with the error
line 6: printf: missing hex digit for \x
and outputs ".__printf_-v_base____________x__x__" to the variable.
I am trying to understand why this is. As the DEBUG trap outputs everything to STDERR, so how can it influence the printf -v function? And how can I solve it?
set -x, it does like exactly the same as your trap commandset -xthis bug does not occur, so it can't be exactly the same.