Hot answers tagged

93 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 ...
Stephen Kitt's user avatar
52 votes
Accepted

How to invoke vim editor and pipe output to bash

vipe is a program for editing pipelines: command1 | vipe | command2 You get an editor with the complete output of command1, and when you exit, the contents are passed on to command2 via the pipe. ...
muru's user avatar
  • 72.3k
50 votes

Direct output to pipe and stdout

tee always writes to its standard output. If you want to send the data to a command in addition to the terminal where the standard output is already going, just use process substitution with that ...
Gilles 'SO- stop being evil''s user avatar
46 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 (...
Bradd Szonye's user avatar
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 )...
pLumo's user avatar
  • 22.5k
42 votes

How to trick a command into thinking its output is going to a terminal

A history of toolsets You are not the first person to want such a tool. People have been wanting such tools for 30 years. And they've existed for almost that long, too. The earliest tool for this ...
JdeBP's user avatar
  • 68.4k
41 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 ...
Gilles 'SO- stop being evil''s user avatar
35 votes
Accepted

How to trick a command into thinking its output is going to a terminal

You might get what you need by using unbuffer. unbufferis a tcl / expect script. Look at the source if you want. Also note the CAVEATS section in man. Also note that it does not execute aliases such ...
Runium's user avatar
  • 28.7k
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 ...
Gilles 'SO- stop being evil''s user avatar
31 votes
Accepted

vim: Force specific syntax via command-line argument

You can use: vim -c 'set syntax=sql' -
Lambert's user avatar
  • 12.6k
31 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 ...
Michael Homer's user avatar
30 votes
Accepted

Direct output to pipe and stdout

Your assumption: fortune | tee >(?stdout?) | pbcopy won't work because the fortune output will be written to standard out twice, so you will double the output to pbcopy. In OSX (and other systems ...
cuonglm's user avatar
  • 153k
28 votes

How to trick a command into thinking its output is going to a terminal

You can use socat to start your process with a pty connected, and get socat to connect the other end of the pty to a file. Which AFAIU is exactly what you asked: socat EXEC:"my-command",pty ...
Guss's user avatar
  • 12.5k
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 ...
Kusalananda's user avatar
  • 331k
25 votes
Accepted

Concatenate contents from stdout and from file

ls -a | grep text1 | cat file2.txt - The - stands for standard input. Alternatively you may write ls -a | grep text1 | cat - file2.txt to have the output in different order. Yet another ...
jimmij's user avatar
  • 47k
24 votes

How to invoke vim editor and pipe output to bash

You can do this from within Vim: :w !pandoc -o file.pdf Or even write the buffer into a complex pipeline: :w !grep pattern | somecommand > file.txt And then you can exit Vim without saving: :q!...
Wildcard's user avatar
  • 36.5k
22 votes

How to trick a command into thinking its output is going to a terminal

There is also a nice solution posted here on Super User by KarlC: Compile a small shared library: echo "int isatty(int fd) { return 1; }" | gcc -O2 -fpic -shared -ldl -o isatty.so -xc - Then tell ...
Amir's user avatar
  • 1,641
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 ...
Kusalananda's user avatar
  • 331k
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
muru's user avatar
  • 72.3k
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/...
Piotr Jurkiewicz's user avatar
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 ...
Sergiy Kolodyazhnyy's user avatar
20 votes

How to invoke vim editor and pipe output to bash

Running in a pipeline Try: quickedit() ( trap 'rm ~/temp$$' exit; vim ~/temp$$ >/dev/tty; cat ~/temp$$ ) The key is that, to be able to use vim normally, vim needs stdout to be the terminal. ...
John1024's user avatar
  • 74.4k
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).
Stephen Kitt's user avatar
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 ...
Kusalananda's user avatar
  • 331k
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): { ...
Stéphane Chazelas's user avatar
18 votes

How to trick a command into thinking its output is going to a terminal

How about using script(1)? For example: script -q -c 'ls -G' out_file Will save the ls output to out_file with the color codes preserved.
sagi's user avatar
  • 291
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 ...
Stephen Kitt's user avatar
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 ...
Stéphane Chazelas's user avatar
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 ...
AnoE's user avatar
  • 575

Only top scored, non community-wiki answers of a minimum length are eligible