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 ...
32
votes
A program that could buffer stdin or file
You can do this with sponge from moreutils. sponge will "soak up standard input and write to a file". With no arguments, that file is standard output. Input given to this command is stored in memory ...
28
votes
Is there any difference between tee and >> when using echo?
There's no difference in the sense that the data in the file will be the same if echo and tee are executed successfully and if the file is writable by the current user.
The tee command would ...
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 ...
21
votes
Accepted
A program that could buffer stdin or file
A poor man's sponge using awk:
awk '{a[NR] = $0} END {for (i = 1; i <= NR; i++) print a[i]}'
If you have tac, you can misuse it too:
... | tac | tac
21
votes
Accepted
systemd: how to redirect stdout to logfile
Use:
[Unit]
Description=My application
[Service]
ExecStart=/usr/bin/java -jar myapp.jar
Type=simple
User=photo
StandardOutput=file:/var/log/logfile
as documented here: https://www.freedesktop.org/...
20
votes
Write Python stdout to file immediately
Variation on the theme of using python's own option for unbuffered output would be to use #!/usr/bin/python -u as first line.
With #!/usr/bin/env python that extra argument not gonna work, so ...
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).
20
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):
{ ...
18
votes
Accepted
Redirecting stdout from two programs
There’s a good chance that prog1 is writing its output to standard error. You can redirect both outputs to a single file with
prog1 prog2 > outfile.txt 2>&1
or you can split the outputs ...
18
votes
Accepted
KeepassXC-cli: Send password to stdout
The solution is to use the -s (--show-protected) and -a (--attributes) flags as follows:
keepassxc-cli show -sa password database entry
-s will display the password instead of PROTECTED , and -a ...
17
votes
Accepted
bash: redirect stderr to file and stdout + stderr to screen
With a recent bash, you can use process substitution.
foo 2> >(tee stderr.txt)
This just sends stderr to a program running tee.
More portably
exec 3>&1
foo 2>&1 >&3 | ...
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
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 ...
16
votes
Accepted
Can writing to stdout place backpressure on a process?
A pipe has a limited buffer size. If the producer goes ahead of the consumer, the data progressively fills the pipe's buffer. If the buffer is filled up, the write call in the producer blocks until ...
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
Best way to `tee` to stdout and to a process
You do it exactly the way you have shown:
somecommand | tee >(othercommand)
The output of somecommand would be written to the input of othercommand and to standard output.
The issue with your ...
14
votes
How to trick a command into thinking its output is going to a terminal
Unsatisfied with the solutions presented here so far, I released the python. She was effective. This solution doesn't require setuid permissions or any actually-insane monkey-patching with shared ...
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
at some time from now do something (and maybe also show result in console)
The correct at usage is at <timespec>, where <timespec> is the time specification. You can also use the -f option to specify the file containing the commands to execute. If -f and no ...
13
votes
Accepted
How do you read STDOUT into variables in bash?
Since the network speeds are not integers, we need to supplement with other tools such as awk to process the numbers. Try:
ifstat -ni wlp7s0 | awk 'NR>2{if ($1+0<100 && $2+0<100) ...
13
votes
How do you read STDOUT into variables in bash?
John1024 is right about floating point numbers, but we can just truncate the numbers. With plain bash:
n=0
LC_NUMERIC=C ifstat -i $interface \
| while read -r in out; do
((++n < 2)) && ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
stdout × 479bash × 135
stderr × 121
io-redirection × 86
pipe × 77
stdin × 76
shell × 66
shell-script × 56
linux × 41
terminal × 34
tee × 30
grep × 21
file-descriptors × 20
process × 19
files × 18
output × 18
ssh × 16
command-line × 15
buffer × 15
logs × 14
echo × 12
systemd × 11
cron × 10
io × 10
scripting × 9