95
votes
Do progress reports/logging information belong on stderr or stdout?
POSIX defines standard error as
for writing diagnostic output
This doesn't limit its use to error messages only. I would consider progress information as diagnostic output, so it belongs on ...
47
votes
Accepted
Do progress reports/logging information belong on stderr or stdout?
Posix defines the standard streams thus:
At program start-up, three streams shall be predefined and need not be opened explicitly: standard input (for reading conventional input), standard output (...
43
votes
Accepted
How to redirect stdout to a file, and stdout+stderr to another one?
Problem is that when you redirect your output, it's not available anymore for the next redirect. You can pipe to tee in a subshell to keep the output for the second redirection:
( cmd | tee -a file2 )...
42
votes
Accepted
POSIX compliant way to redirect stdout and stderr to a file
You can check the POSIX specification. The language isn't always easy to follow, but if you know what you're looking for, you can see whether it's there or not.
POSIX includes the redirection ...
35
votes
Accepted
What prevents stdout/stderr from interleaving?
They do interleave! You only tried short output bursts, which remain unsplit, but in practice it's hard to guarantee that any particular output remains unsplit.
Output buffering
It depends how the ...
23
votes
Accepted
what is the difference between `>> /dev/stderr` (with the white space) and `>&2`?
>& n is shell syntax to directly duplicate a file descriptor. File descriptor 2 is stderr; that's how that one works. You can duplicate other file descriptors as well, not just stderr. You can'...
22
votes
Is it safe to redirect stdout and stderr to the same file without file descriptor copies?
What happens when you do
some_command >>file 2>>file
is that file will be opened for appending twice. This is safe to do on a POSIX filesystem. Any write that happens to the file when ...
20
votes
Accepted
Why can issuing the same command create more output in tty than in pts/gnome-terminal?
They're coming from the kernel. You'll see them also by running
dmesg
Kernel messages are displayed on virtual console by default; they aren't in X terminal emulators (such as GNOME Terminal).
19
votes
Accepted
When to use standard error stream in command-line application?
Yes, do display a message on stderr when the wrong arguments are used. And if that also causes the application to exit, exit with non-zero exit status.
You should use the standard error stream for ...
19
votes
Accepted
display STDOUTs before STDERR?
You'll need to hold the stderr output somewhere anyway to be able to display it at the end.
A file comes to mind:
fff 2> file; cat file >&2
Or memory (here using sponge from moreutils):
{ ...
17
votes
How to redirect stdout to a file, and stdout+stderr to another one?
With zsh:
cmd >& out+err.log > out.log
In append mode:
cmd >>& out+err.log >> out.log
In zsh, and provided the mult_ios option has not been disabled, when a file ...
16
votes
Accepted
Should I output program's name when warning or error occurs?
It is common practice to save the 0th argument passed to a C program main and use that as the parameter for perror — for simple programs:
#include <stdio.h>
#include <stdlib.h>
int ...
16
votes
Do progress reports/logging information belong on stderr or stdout?
By the principle of exclusion, it can only go to stderr. Yes, I know you asked about an official specification, which I cannot present you beyond the link to the POSIX specification, given by Stephen ...
15
votes
Do progress reports/logging information belong on stderr or stdout?
POSIX is slightly more concrete about "diagnostic information" in Shell and Utilities, 1.4: Utility Description Defaults (emphasis mine):
STDERR
The STDERR section describes the standard error ...
15
votes
Accepted
what is the difference between >&2 and &>2
Read the Redirection section of the manual carefully: https://www.gnu.org/software/bash/manual/bashref.html#Redirections
The cmd >&2 form is described in section "3.6.8 Duplicating File ...
15
votes
Accepted
How can I use sed to edit bash debug output? (bash -x)
The + is the PS4 prompt. Set it to an empty string:
#!/bin/bash
PS4=''
set -x
echo 'This is a script that has debugging turned on'
Testing:
$ bash script.sh
echo 'This is a script that has ...
14
votes
Accepted
How to redirect stderr in a variable but keep stdout in the console
Just use:
{ err=$(cmd 2>&1 >&3 3>&-); } 3>&1
To get the stderr of cmd while leaving its stdout untouched (here by using fd 3 to bring the original stdout (copied with 3&...
13
votes
Accepted
How do I get the output of a cron script run from my home directory?
Add to the end of your cron table entry: >> /home/myuser/myscript.log 2>&1
This will capture the output to a log file. By default, the output is mailed using the local mailer daemon to ...
13
votes
Accepted
How can I remove this annoying message: "Failed to find cgroup2 mount"?
You have your redirection at the wrong point in the pipeline. Presumably the error comes from the ss command, so that is where you should hide the error output. Or you can group the output and ...
12
votes
Redirect STDERR and STDOUT to different variables without temporary files
On Linux and with shells that implement here-documents with writable temporary files (like zsh or bash versions prior to 5.1 do), you can do:
{
out=$(
chmod u+w /dev/fd/3 && # needed for ...
11
votes
Copy stdout and stderr to a log file and leave them on the console within the script itself
I know this is an old post, but why not just do this?
echo "hi" >> log.txt #stdout -> log
echo "hi" | tee -a log.txt #stdout -> log & stdout
echo "hi" &>> log.txt #stdout &...
11
votes
Accepted
Separate stdout and stderr for `docker run`?
The problem for you is the '-t' option. After removing it, stderr should work as expected:
$ docker run -i --rm alpine sh -c 'echo this is stdout; echo this is stderr >&2' 2>stderr
this is ...
10
votes
Accepted
where is the location of stdin, stdout, stderr file descriptor in AIX(unix)
The question is based on a misconception about the generality of proc filesystems. Systems which implement this (such as Solaris and Linux) have special devices which may be used for scripting, ...
10
votes
Accepted
/dev/stderr in systemd service
If there was no stderr, you wouldn't see the error.
Here, the error is about not being able to open the /dev/stderr file, which on Linux is a symlink to the actual file that is open on the fd 2 (...
10
votes
Accepted
How to make bash substitution $(<"filename") silent
Wrapping the assignment into a compound block and using a redirection on that seems to work:
{ var=$(<"$file"); } 2>/dev/null;
e.g.
$ echo hello > test1; rm -f test2
$ file=test1; { var=$(&...
9
votes
Accepted
Redirect bash stdout+stderr to one file and stderr to another file
With zsh (and zsh only) and its multios feature:
your-cmd 2> stdout+stderr.log >&2 2> stderr.log
As fd 2 is redirected twice, zsh implements an internal tee to have it sent to both files....
9
votes
Accepted
Execute command and store everything to variable in bash
TAROUTPUT=$(tar -cf arch.tar /path/to/dir 2>&1)
this_is_the_tar_exit_code=$?
8
votes
Should I output program's name when warning or error occurs?
If a program is invoked as part of a script in which many other programs are invoked, and if it doesn't print its name, then users will find it hard(er) to figure out where the error is coming from.
(...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
stderr × 243stdout × 121
bash × 85
io-redirection × 78
shell × 36
shell-script × 35
pipe × 26
linux × 18
stdin × 14
grep × 11
file-descriptors × 11
output × 10
ssh × 8
tee × 8
terminal × 7
command-line × 6
find × 6
process × 6
scripting × 5
zsh × 5
cron × 5
logs × 5
nohup × 5
exit-status × 5
systemd × 4