10
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 .

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Balaganesh
  • 129
  • 1
  • 1
  • 3

3 Answers3

26

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.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • "stdout is a normal file, created in the same directory where you're running the command." This is not a good way to write an explanation. Better: In the 1st case you redirected stdout to a file, a file which you somewhat foolishly decided to name "stdout". This file has nothing to do with the unix concept of "stdout". You should have called the file test.out or something that does not cause confusion of the concepts.. – reikred Aug 11 '23 at 17:01
8

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

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
5

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621