bash stdout 和 /dev/stdout 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30780780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between stdout and /dev/stdout
提问by Balaganesh
bala@hp:~$ echo "Hello World" > stdout
bala@hp:~$ cat stdout
Hello World
bala@hp:~$ echo "Hello World" > /dev/stdout
Hello World
Kindly clarify what is the difference between stdout
and /dev/stdout
请澄清之间有什么区别stdout
和/dev/stdout
Note :
笔记 :
bala@hp:~$ file stdout
stdout: ASCII text
bala@hp:~$ file /dev/stdout
/dev/stdout: symbolic link to `/proc/self/fd/1'
Kindly help to know the difference .
请帮助了解差异。
回答by Sourav Ghosh
In your case
在你的情况下
stdout
is a normal file, created in the same directory where you're running the command.So, when you're redirecting the output of
echo
tostdout
, it is written to the file. You need to docat
(as example, here you did) in order to see the content on screenhere./dev/stdout
is a devicefile, which is a link to/proc/self/fd/1
, which means it is referring to the file descriptor 1 held by the current process.So, when you're redirecting the output of
echo
to/dev/stdout
, it is sent to the standard output (the screen) directly.
stdout
是一个普通文件,在您运行命令的同一目录中创建。因此,当您将
echo
to的输出重定向时stdout
,它会被写入文件。您需要执行cat
(例如,您执行此操作)才能在此处查看屏幕上的内容。/dev/stdout
是一个设备文件,它是指向 的链接/proc/self/fd/1
,这意味着它指的是当前进程持有的文件描述符 1。因此,当您将
echo
to的输出重定向时/dev/stdout
,它会直接发送到标准输出(屏幕)。
回答by paxdiablo
stdout
on its own is just a file in the current directory, no different to finances.txt
. It has nothing to do with the standard output of the process other than the fact you're redirecting standard output to it.
stdout
本身只是当前目录中的一个文件,与finances.txt
. 除了您将标准输出重定向到它的事实之外,它与进程的标准输出无关。
On the other hand, /dev/stdout
is a link to a special file on the procfs
file system, which represents file descriptor 1 of the process using it1.
另一方面,/dev/stdout
是指向procfs
文件系统上特殊文件的链接,它表示使用它的进程的文件描述符 1 1。
Hence it has a very real connection to the process standard output.
因此,它与过程标准输出有着非常真实的联系。
1The procfs
file system holds all sorts of wondrous information about the system and all its processes (assuming a process has permissions to get to them, which it should have for /proc/self
).
1该procfs
文件系统拥有各种有关系统的所有进程(假设一个进程的权限才能上,它应该有一个奇妙的信息/proc/self
)。
回答by Some programmer dude
One is a normal file, no different from any other normal file like e.g. ~/foobar.txt
.
一个是普通文件,与任何其他普通文件(例如~/foobar.txt
.
The other is a symbolic link (like one you can create with ln -s
) to a special virtual file that represents file descriptor 1 in the current process.
另一个是ln -s
到一个特殊虚拟文件的符号链接(就像你可以创建的那样),该文件代表当前进程中的文件描述符 1。