mainThe GNU Bourne-Again SHell - Support: sr #110807, Builtin set -x causes some...


sr #110807: Builtin set -x causes some commands not to be executed

Submitter:  LGTR <lgtr>
Submitted:  Fri 30 Dec 2022 04:33:37 PM UTC
Category:  None Priority:  5 - Normal
Severity:  3 - Normal Status:  Invalid
Privacy:  Public Assigned to:  None
Open/Closed:  Open Operating System:  None
* Mandatory Fields

Add a New Comment Rich Markup

Sat 28 Jan 2023 01:59:03 AM UTC, comment #2: 

That is how xtrace should work, it always writes to the current stderr.

If you want to xtrace a script that uses *$(2>&1) (e.g. scripts that use the *dialog utility), you can always export BASH_XTRACEFD set to a file descriptor that duplicates stderr; e.g. in your case:

(exec {BASH_XTRACEFD}>&2; export BASH_XTRACEFD; bash -x ./

Or you can write *exec {BASH_XTRACEFD}>&2* at the top of your script.

Emanuele Torre <emanuele6>
Sat 07 Jan 2023 08:54:24 PM UTC, comment #1: 

You explicitly ask for the tracing output to be included in the command substitution output (2>&1 after the subshell command). Why would it not end up as part of the value of the variable?

Chet Ramey <chet>
Group administrator
Fri 30 Dec 2022 04:33:37 PM UTC, original submission:  

Script reproducing the issue with GNU bash 5.2.15:

#!/usr/bin/env bash
foo=$( ( dd if=/dev/null bs=1024 count=10 2>/dev/null | cat 1>/dev/null ) 2>&1 )
printf 'foo is \"%s\"\n' "$foo"
if [ ! "$foo" ]; then echo "bravo is $bravo"; fi

First run the script normally:

$ bash +x
foo is ""
bravo is 2

With '-x':

$ bash -x
+ bravo=2
+ foo='++ dd if=/dev/null bs=1024 count=10
++ cat'
+ printf 'foo is \"%s\"\n' '++ dd if=/dev/null bs=1024 count=10
++ cat'
foo is "++ dd if=/dev/null bs=1024 count=10
++ cat"
+ '[' '!' '++ dd if=/dev/null bs=1024 count=10
++ cat' ']

bash manual does not mention any command or result modification because of the '-x' builtin:,%2Dx,-Print%20a%20trace

LGTR <lgtr>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by emanuele6 (Posted a comment)
  • -email is unavailable- added by chet (Posted a comment)
  • -email is unavailable- added by lgtr (Submitted the item)

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.


    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-01-07 chet StatusNone Invalid

    Back to the top

    Powered by Savane 3.13-ee12.
    Corresponding source code