Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print to stderr in fish shell?

In a fish-shell script, how do you print an error message to stderr?

For example, this message should go to the stderr stream rather than the default stdout stream.

echo "Error: $argv[1] is not a valid option"
like image 702
Dennis Avatar asked Jun 10 '17 15:06

Dennis


People also ask

How do I send a message error to stderr?

The correct thing to do is 2>errors. txt 1>&2 , which will make writes to both stderr and stdout go to errors. txt , because the first operation will be "open errors. txt and make stderr point to it", and the second operation will be "make stdout point to where stderr is pointing now".

Should warnings be printed to stderr?

The real question is: if someone were to redirect the output of your script to a file, would you want the warning placed in the file, or directed to the user? If you're expecting the user to take some action as a result of the warning, it should go to STDERR.

Do warnings go to stderr?

Warning messages are normally written to sys. stderr , but their disposition can be changed flexibly, from ignoring all warnings to turning them into exceptions.


1 Answers

You can redirect the output to stderr, for example:

echo "Error: $argv[1] is not a valid option" 1>&2

As a reference, here are some common IO-redirections that work in fish*.

foo 1>&2 # Redirects stdout to stderr, same as bash

bar 2>&1 # Redirects stderr to stdout, same as bash

bar ^&1  # Redirects stderr to stdout, the fish way using a caret ^

* The file descriptors for stdin, stdout, and stderr are 0, 1, and 2.
* The & implies you want to redirect to a file stream instead of a file.
* Comparison of redirection in various shells (bash, fish, ksh, tcsh, zsh)

like image 186
Dennis Avatar answered Sep 25 '22 00:09

Dennis