Skip to main content
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
  • 23.2k
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 ...
Gilles 'SO- stop being evil''s user avatar
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 ...
Gilles 'SO- stop being evil''s user avatar
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
  • 356k
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>...
Stéphane Chazelas's user avatar
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
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 ...
Stéphane Chazelas's user avatar
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 ...
glenn jackman's user avatar
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 ...
Kusalananda's user avatar
  • 356k
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 ...
Ángel's user avatar
  • 3,814
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 ...
rosarote elfe's user avatar
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 ...
Stéphane Chazelas's user avatar
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 ...
bxm's user avatar
  • 5,210
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 (...
Stéphane Chazelas's user avatar
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=$?
Hauke Laging's user avatar
  • 94.5k
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 ...
grawity's user avatar
  • 15.2k
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 ...
Keiji's user avatar
  • 521
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 ...
Stéphane Chazelas's user avatar
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=$(&...
ilkkachu's user avatar
  • 148k
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 ...
cas's user avatar
  • 83.9k
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 ...
Marcus Müller's user avatar
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 -...
styshoo's user avatar
  • 181
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 ...
ctrl-alt-delor's user avatar
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 ...
JdeBP's user avatar
  • 71.9k
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 ...
Kusalananda's user avatar
  • 356k
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 | +--+--+ ...
Ljm Dullaart's user avatar
  • 5,053
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 ...
ilkkachu's user avatar
  • 148k
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 ...
RudiC's user avatar
  • 9,049

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