Sample Header Ad - 728x90

Additional file descriptor for debugging and piped output (logging, metrics, etc)

3 votes
1 answer
781 views
For a bash script project, I write human-readable log info to stdout/stderr. Additionally, I want to write formatted metrics to a third stream that will be discarded by default but can be redirected for piped processing. Is the approach doing this with an additional file descriptor advised?
exec 3> /dev/null
echo "This is stdout"
echo "This is stderr" >&2
echo "This is fd3" >&3
I'm fine with the third line not showing up under normal conditions. However, when used in a certain toolchain I want to pipe these messages. Simple example:
$ bash example.sh 3>&1
This is stdout
This is stderr
The third line does not appear as the console output. What am I doing wrong? Is there a solution to this? Is another approach advised?
Asked by Mazzen (33 rep)
Nov 7, 2020, 11:12 AM
Last activity: Nov 7, 2020, 01:12 PM