Piping both stdout and stderr in bash?

It seems that newer versions of bash have the &> operator, which (if I understand correctly), redirects both stdout and stderr to a file (&>> appends to the file instead, as Adrian clarified). What’s the simplest way to achieve the same thing, but instead piping to another command? For example, in this line: cmd-doesnt-respect-difference-between-stdout-and-stderr | … Read more

How to store standard error in a variable

Let’s say I have a script like the following: useless.sh echo “This Is Error” 1>&2 echo “This Is Output” And I have another shell script: alsoUseless.sh ./useless.sh | sed ‘s/Output/Useless/’ I want to capture “This Is Error”, or any other stderr from useless.sh, into a variable. Let’s call it ERROR. Notice that I am using … Read more

How to redirect and append both standard output and standard error to a file with Bash

To redirect standard output to a truncated file in Bash, I know to use: cmd > file.txt To redirect standard output in Bash, appending to a file, I know to use: cmd >> file.txt To redirect both standard output and standard error to a truncated file, I know to use: cmd &> file.txt How do … Read more

How to print to stderr in Python?

There are several ways to write to stderr: # Note: this first one does not work in Python 3 print >> sys.stderr, “spam” sys.stderr.write(“spam\n”) os.write(2, b”spam\n”) from __future__ import print_function print(“spam”, file=sys.stderr) That seems to contradict zen of Python #13 †, so what’s the difference here and are there any advantages or disadvantages to one … Read more