linuxbashcommand-line

bash: BASH_XTRACEFD: 5: invalid value for trace file descriptor


I want to do the same thing as shell - Suppress the bash execution trace (set -x) from the outside of the script - Unix & Linux Stack Exchange's one.

When I doing Bash debugging, sometimes a command (spinner 'Now loading... ' &) occurred and the command make debugging output dirty because the command is using while loop so a lot of amount of output there is.

In this case, I want to suppress the output of the command by something like spinner 'Now loading... ' 5>/dev/null &.

I searched how to do it and I found solution that is BASH_XTRACEFD.

But I try to define the variable in a command line, there is a error occurred as following.

$ export BASH_XTRACEFD="5"
bash: BASH_XTRACEFD: 5: invalid value for trace file descriptor

I guess the reason why the error occurred is file descriptor is closed or something wrong. but I have no idea what have to do to solve this issue.


Solution

  • The example you linked to shows the correct way of doing it, which is in a single line before creating a new shell by running a script.

    Your problem is that you're currently doing this in a interactive shell which does not have a file descriptor 5. You can fix the problem by replacing your shell and creating that file descriptor beforehand, if you just want to discard all of set -x, then you can use

    exec 5> export BASH_XTRACEFD=5 That should take care of the problem you demonstrated, however I feel like you're just trying to redirect stderr, so instead do

    --> ls -l foo
    ls: cannot access foo: No such file or directory
    root@mybox[/tmp] Wed Jul 03 <11:17:46>
    --> ls -l foo 2>/dev/null # Notice the 2>/dev/null after the command
    root@mybox [/tmp] Wed Jul 03 <11:17:50>
    

    And that will supress the spinner in case it's writing to stderr.