2

I want to set all debugging output to a log file. So I checked command line - bash: set -x logs to file | Ask Ubuntu and followed the instructions.

test-script:

#!/bin/bash
exec 5 >/var/log/test.log
export BASH_XTRACEFD=5
main(){
set -x
echo hello
set +x
}
main

run:

./test-script

returns:

exec: 5: not found

The manual shows you can do this as well, so why is the above not working?

0
3

The error gets you halfway there:

exec: 5: not found

exec tried to execute a command called 5, and didn't find it.

The syntax for redirecting a file descriptor calls for no space between the file descriptor number and the redirection operator, like so:

exec 5> /var/log/test.log
4

There should be no space between 5 and > on the exec line.

With the space, the script tries to execute a command called 5, which is why you get "not found".

Without the space, you attach file descriptor 5 to the given file name for output.

Additionally, there is no need to export the variable BASH_XTRACEFD, it's enough to just set it:

#!/bin/bash 

exec 5>test.log   

BASH_XTRACEFD=5

main () {           
    set -x        
    echo hello    
    set +x        
}

main 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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