Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pipe both, stdout and stderr in the fish shell

Tags:

fish

I know this has been an issue for a while and I found a lot of discussion about it, however I didn't get which would be finally a way to get it done: pipe both, stdout and stderr. In bash, this would be simply:

cmd 2>&1 | cmd2
like image 587
Anton Harald Avatar asked May 30 '16 17:05

Anton Harald


People also ask

Does pipe take stderr?

According to "Linux: The Complete Reference 6th Edition" (pg. 44), you can pipe only STDERR using the |& redirection symbols. Presumably, only the lines printed to STDERR will be indented.

How do I redirect stderr and stdout in bash?

Understanding the concept of redirections and file descriptors is very important when working on the command line. To redirect stderr and stdout , use the 2>&1 or &> constructs.


2 Answers

That syntax works in fish too. A demo:

$ function cmd1
      echo "this is stdout"
      echo "this is stderr" >&2
  end

$ function cmd2
      rev
  end

$ cmd1 | cmd2
this is stderr
tuodts si siht

$ cmd1 &| cmd2
rredts si siht
tuodts si siht

Docs: https://fishshell.com/docs/current/language.html#redirects

like image 159
glenn jackman Avatar answered Oct 13 '22 21:10

glenn jackman


There's also a handy shortcut, per these docs

&>

Here's the relevant quote (emphasis and white space, mine):

As a convenience, the redirection &> can be used to direct both stdout and stderr to the same destination. For example:

echo hello &> all_output.txt

redirects both stdout and stderr to the file all_output.txt. This is equivalent to echo hello > all_output.txt 2>&1.

like image 36
Navelgazer Avatar answered Oct 13 '22 23:10

Navelgazer