Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between stdout and /dev/stdout

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

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 .

like image 907
Balaganesh Avatar asked Jun 11 '15 12:06

Balaganesh


3 Answers

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 to stdout, it is written to the file. You need to do cat (as example, here you did) in order to see the content on screen here.

  • /dev/stdout is a device file, 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.

like image 58
Sourav Ghosh Avatar answered Sep 28 '22 09:09

Sourav Ghosh


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.

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.

Hence it has a very real connection to the process standard output.


1 The 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).

like image 30
paxdiablo Avatar answered Sep 28 '22 09:09

paxdiablo


One is a normal file, no different from any other normal file like e.g. ~/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.

like image 43
Some programmer dude Avatar answered Sep 28 '22 08:09

Some programmer dude