How does `exec 2>>` or `BASH_XTRACEFD` help separate debug output from normal output?
AdvancedConceptArguments, I/O, here-docs, process substitution and subshells
Answer
set -x writes traces to stderr by default, mixing them with real errors. Bash can send trace output to a dedicated fd:
exec {xfd}>>/var/log/deploy/trace.log
BASH_XTRACEFD=$xfd
PS4='+ ${BASH_SOURCE##*/}:${LINENO}:${FUNCNAME[0]:-main}: '
set -x
Now stderr contains only real errors (and goes to the alerting pipeline), while the trace file has every command with file, line and function. Remember that traces include expanded secrets; wrap sensitive sections in set +x and set -x.
More on Arguments, I/O, here-docs, process substitution and subshells
How does `getopts` work? Show a complete example.IntermediateWhat is the difference between `getopts` and `getopt`?AdvancedExplain file descriptors 0, 1 and 2 and the main redirection operators.BeginnerWhy is `cmd 2>&1 > file` different from `cmd > file 2>&1`?AdvancedWhat is a here-document, and what is the difference between `<<EOF`, `<<'EOF'` and `<<-EOF`?BeginnerWhat is a here-string (`<<<`)?Intermediate