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 )...
43
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 ...
36
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 ...
31
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 ...
23
votes
Why does "seq 1000000 | tee /dev/stdout" produce more single-digit numbers than expected?
Having both of tee's outputs going to the same pipe creates tearing within lines because they don't start/end at the boundaries of read/write blocks.
tee, reads its input and writes its output by ...
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
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 ...
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
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>...
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
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 ...
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
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 descriptor ...
16
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
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 ...
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 to save an output of airodump-ng to a file?
Check
man airodump-ng.
You want the -w option.
airodump-ng -w myOutput --output-format csv mon0
Generates a .csv file of the screendump with the output from airodump-ng one line per station.
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)) && ...
13
votes
Accepted
Why is mawk's output (STDOUT) buffered even though it is the terminal?
It's not that it's buffering its output.
mawk is the only utility that I know that buffers its input.
See also https://github.com/ThomasDickey/original-mawk/issues/41#issuecomment-241070898
In other ...
13
votes
Accepted
How do I reliably capture the output of `ls` in this script?
Assuming the “if there is only one in the current directory” part is correct (i.e. you only care about files in the current directory), you can avoid using ls, let alone find:
#!/usr/bin/bash -
shopt ...
12
votes
Is there any difference between tee and >> when using echo?
Tee is primarily used to redirect the output to multiple files instead of invoking a copy command separately.
E.g.:
wc -l | tee -a file1.txt file2.txt file3.txt
You can elevate privilege to the tee ...
12
votes
Accepted
A command wants file paths. How can I give it stdin for the "infile" and stdout for the "outfile"?
You can use special files /dev/stdin or /dev/fd/0 for stdin, and /dev/stdout or /dev/fd/1 for stdout.
In your example:
echo "abc" | foo -in /dev/fd/0 -out /dev/fd/1
The availability of these special ...
12
votes
How to print to stdout after exec >/dev/null
Any echo would print to stdout. It's just that your stdout now points to /dev/null. Point being that the original stdout is in no way special, or more "true" than the stdout you have after a ...
12
votes
How do I reliably capture the output of `ls` in this script?
You can't use ls like that. Those *.[aA][vV][iI] are expanded by the shell, not ls, the -R for ls is for it to Recursively list the contents of directories, for which you need to pass it the path of ...
11
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=$?
10
votes
Communicate backwards in a pipe
On systems where pipes are bidirectional (NetBSD, FreeBSD, SVR4-derived Unices (all those where pipes use STREAMS at least), but not Linux):
node foo.js <&1 | node bar.js >&0
On systems ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
stdout × 486bash × 136
stderr × 122
io-redirection × 88
pipe × 78
stdin × 76
shell × 66
shell-script × 59
linux × 42
terminal × 34
tee × 32
grep × 21
file-descriptors × 20
process × 19
output × 19
files × 18
ssh × 16
command-line × 15
buffer × 15
logs × 14
systemd × 12
echo × 12
cron × 10
io × 10
scripting × 9