Difference between stdout and /dev/stdout

17,091

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
17,091
Admin
Author by

Admin

Updated on June 12, 2022

Comments

  • Admin
    Admin almost 2 years
    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 .