如何在鱼壳中打印到stderr?

Den*_*nis 5 shell stdout stderr io-redirection fish

在fish-shell脚本中,如何向stderr打印错误消息?

例如,此消息应该转到stderr流而不是默认的stdout流.

echo "Error: $argv[1] is not a valid option"
Run Code Online (Sandbox Code Playgroud)

Den*_*nis 10

您可以将输出重定向到stderr,例如:

echo "Error: $argv[1] is not a valid option" 1>&2
Run Code Online (Sandbox Code Playgroud)

作为参考,这里有一些常见的IO重定向,适用于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 ^
Run Code Online (Sandbox Code Playgroud)

*stdin,stdout和stderr的文件描述符分别为0,1和2.
*&暗示您要重定向到文件流而不是文件.
*各种shell中重定向的比较(bash,fish,ksh,tcsh,zsh)

  • 您不需要“1”——只需添加“>&2”也可以,因为从标准输出重定向是默认的。(不允许有空格 - `> &2` 不起作用。) (3认同)