How to Quickly Determine if Cli Output Is Stdout or Stderr
Today i was trying to use gitleaks
and i wanted to generate reports and save the summary to a file. I’m a bit confused since when i piped the command to a file it was empty. Turns out that it piped the summary to stderr
. Here’s a simple bash function you can add to your .bashrc
to quickly determine if your output is from stdout
or stderr
.
jstd() {
"$@" 2> >(sed 's/^/2: /') > >(sed 's/^/1: /')
}
Sample Outputs :
anya@x390:/tmp/jstd$ jstd ls
1: file1
1: file2
1: file3
anya@x390:/tmp/jstd$ jstd ls nonexsistent file
2: ls: cannot access 'nonexsistent': No such file or directory
2: ls: cannot access 'file': No such file or directory
anya@x390:/tmp/jstd$ jstd gitleaks detect --source hellogitworld/
2:
2: ○
2: │╲
2: │ ○
2: ○ ░
2: ░ gitleaks
2:
2: 10:35AM INF 51 commits scanned.
2: 10:35AM INF scan completed in 8.09ms
2: 10:35AM INF no leaks found
anya@x390:/tmp/jstd$ ls