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 ...
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
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):
{ ...
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 ...
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 ...
15
votes
Recommended way for a Linux app to inform user of an exception
First of all, not all errors are equal.
"The username and password you are using to authenticate were rejected by the remote server" is quite different than "The machine is out of ...
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
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 ...
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 ...
11
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 (...
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=$?
11
votes
Recommended way for a Linux app to inform user of an exception
due to unstable state of the app (SIGSEGV or SIGBUS may not have any recovery)
I think it is still possible in a SIGSEGV handler to fork+exec a "crash handler" process (which then would ...
11
votes
Recommended way for a Linux app to inform user of an exception
I'm going to try and directly answer the questions [I think] you're actually asking.
How should a program handle UNIX signals?
Generally speaking, it shouldn't.
UNIX signals are a somewhat dated ...
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 ...
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
Make apt install ignore missing packages
apt-get (and apt) have a -m option to ignore missing packages. From man apt-get:
-m, --ignore-missing, --fix-missing
Ignore missing packages; if packages cannot be retrieved or fail the
integrity ...
9
votes
Recommended way for a Linux app to inform user of an exception
Modern desktop environments implement the notification dbus API, concretely org.freedesktop.Notifications.Notify. You can interact with that directly via dbus (which is an RPC standard used throughout ...
8
votes
nohup: ignoring input and redirecting stderr to stdout
In my situation, I redirect stdout and stderr, but it also displays following in the log file:
nohup: ignoring input
To remove that warning, you have to also redirect stdin as follows:
nohup java -...
8
votes
Accepted
How (and why) use stderr for both reading and writing?
I was surprised at first. However after reading the answers, and doing a little investigation, it seems simple. So here is what I have found. (in the end there was no surprise.)
Before redirection ...
8
votes
Accepted
What were the original reason(s) for adding a "standard error" which is separate from "standard output"?
There's Steve C. Johnson's 2013 account of this, as a user, where users complain about phototypesetting and — lo! — the problem is fixed two days later. But Douglas McIlroy told the tale slightly ...
8
votes
Why can't I get pgrep output right to variable on bash script?
You are getting the pgrep output in your status variable. It's just not the output that you expect it to be.
pgrep outputs the process IDs (PIDs) of the processes matching the pattern that you give ...
8
votes
Standard error to standard output - is there other options beside redirection?
In my early days of Unix, it was explained to me that a pipe works just like plumbing.
A process basically has 3 connection points for your plumping:
stdin
|
+--+--+
...
7
votes
Ambiguous output redirect
If you're running (t)csh, you get Ambiguous output redirect. if you try to set up two conflicting redirections:
> echo foo > a > b
Ambiguous output redirect.
In Bash, you could get a similar ...
7
votes
Accepted
What is non-standard input/output/error
The file descriptors 0, 1, and 2 are provided by default when a process is execed, and associated with stdin, stdout, and stderr. If a process needs additional I/O channels, it opens non-standard file ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
stderr × 246stdout × 122
bash × 85
io-redirection × 78
shell × 36
shell-script × 35
pipe × 26
linux × 18
stdin × 14
grep × 12
file-descriptors × 11
output × 11
tee × 8
ssh × 7
terminal × 7
command-line × 6
find × 6
process × 6
exit-status × 6
scripting × 5
zsh × 5
cron × 5
logs × 5
systemd × 4
python × 4