2>& 1在这里是什么意思?

时间:2017-12-13 01:01:54

标签: bash shell unix redirect

我在bash中试过这段代码:

 >$(
 >echo bonjour
 >echo bonsoir 2>&1
 >)

就在那之后,结果是:bonsoir, 之后我做了

>$ cat toto

结果是:bonjour,我只想了解2> $ 1在这里做了什么?

4 个答案:

答案 0 :(得分:3)

在谈论重定向之前,我觉得你需要了解一个基本的东西:
Linux命令产生正常输出和错误输出,而unix使您可以自由地将每个输出“重定向”到一个单独的“通道”,称为文件描述符(fd)。

通道/ fd 1用于标准输出,通道/ fd 2用于错误输出。

例如,你可以让命令像这样运行:

$ command 1>output 2>errors #or more simple command >output 2>errors . A simple >output is a shortcut to 1>output

在上面的命令中,屏幕上不会打印任何内容。正常输出将被发送到名为'output'的本地文件,错误将被发送到名为'errors'的本地文件。

现在想象自己有两个名为tty0和tty1的监视器。 您可以使用

之类的东西来区分输出
$ command 1>/dev/tty0 2>/dev/tty1

在这种情况下,命令的正常输出进入通道1 = tty0,错误进入通道2 = tty1。

如果省略重定向并且在shell提示符下运行命令,则通道1和通道2都会重定向到您的唯一屏幕(tty0)。

所以只需在你的shell中运行

$ command

默认等效于

$ command 1>/dev/tt0 2>/dev/tt0

当我们有一个变量赋值(就像你的例子)那么游戏就有点不同了 Shell创建一个临时位置(如果您愿意,可以创建一个变量空间)来保存命令产生的正常输出,但默认情况下错误输出不会被驱动到该变量。

所以对于像这样的变量赋值

variable=$(command) 

以某种方式等于:

command 1>$variable 2>/dev/tt0    #this is not a valid syntax , it is just an example to demonstrate how different ouputs are handled by default.

错误输出一直持续到你的屏幕tty0,除非你明确地将错误输出重定向到其他地方,例如2>file(向文件发送错误)或2>&1 =发送错误输出到同一位置正常输出= =变量

考虑这些测试:

echo "hello world" >hello.txt 
#we create a simple file. Channel number is not given but is considered to be 1 by defauly, so this is equivalent to echo "..." 1>hello.txt 
ls -all
> drwx------    2 9925691 root   4096 Dec 13 01:29 .
> drwxr-xr-x 2802 root    root 253952 Dec 13 01:29 ..
> -rw-r--r--    1 9925691 root     12 Dec 13 01:29 hello.txt

a=$(ls -all hello.txt) #variable assignment ignoring the error output
echo "Lets print the contents of variable 'a' : $a"
> Lets print the contents of variable 'a' : -rw-r--r-- 1 9925691 root 12 Dec 13 01:31 hello.txt

echo "one more test"
b=$(ls -all helo.txt) #there is no file helo.txt so this will produce an error
echo "variable 'b' contains: $b"
> one more test
> variable 'b' contains:  #nothing is printed in normal output &1 so the variable is null
> ls: cannot access 'helo.txt': No such file or directory #this error is printed directly to tty0, is not stored in variable "$a" and thus can not be used later on from your scripts.

echo "another test'
c=$(ls -all helo.txt 2>&1)
echo "variable 'c' contains: $c"
> another test
> variable 'c' contains: ls: cannot access 'helo.txt': No such file or directory #Variable $c contains the error output of the command ls.
> #nothing is printed on your screen directly. is all stored in variable "$c"

答案 1 :(得分:0)

在这些重定向情况下,您可以使用stdout(1)或stderr(2)。运行2>&1时,您将stderr重定向到stdout。为了使事情更容易阅读:

stderr(2) > stdout(1)

运行以下代码时:

>$(
>echo bonjour
>echo bonsoir 2>&1
>)

bonjourbonsoir都应该打印到控制台,因为stdout默认为控制台,并且您将stderr重定向到stdout。由于没有任何错误,两者都应该转到stdout(或控制台)。

答案 2 :(得分:0)

Unix shell使用流来处理输入和输出输出。从0开始,数字引用流。每个shell为您提供3个流

标准输入(stdin)是数字0,是默认输入流(进入脚本的数据)

标准输出(stdout)是数字1,用于正常输出

标准错误(stderr)是数字2,用于发送错误消息

stdout和stderr是惯例。你不必像那样使用它们(但你真的应该尝试)。

2>& 1是shell表示法,用于将流2(stderr)重定向到流2(stdout),以便所有正常输出和错误输出都转到同一个流。例如,如果要将它们都指向同一个文件,这非常有用。

答案 3 :(得分:0)

2>&1是shell表示法,用于将流2(stderr)重定向到流2(stdout),以便所有常规输出和错误输出都进入同一流。例如,如果要将它们都定向到同一个文件,这将很有用。