Linux Command Reference ยท Special Files and Directories
The /dev/stdin, /dev/stdout and /dev/stderr files
Reference for /dev/stdin, /dev/stdout, /dev/stderr and /dev/fd/N: pass standard streams to programs that need a file name, use process substitution, and redirect safely.
/dev/stdin, /dev/stdout and /dev/stderr are file names for a process's own standard streams (file descriptors 0, 1 and 2). They are symbolic links to /proc/self/fd/0, /proc/self/fd/1 and /proc/self/fd/2, and /dev/fd/N gives the same for any open descriptor N.
They matter when a program insists on a file name but you want it to read from a pipe or write to the terminal. Writing -o /dev/stdout or reading /dev/stdin lets such programs take part in pipelines.
echo hello | cat /dev/stdin echo oops > /dev/stderr diff <(sort a.txt) <(sort b.txt) # uses /dev/fd/N
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
| File | Meaning |
|---|---|
/dev/stdin |
Standard input of the current process (descriptor 0). |
/dev/stdout |
Standard output (descriptor 1). |
/dev/stderr |
Standard error (descriptor 2). |
/dev/fd/N |
Open descriptor N of the current process. |
/proc/PID/fd/N |
Descriptor N of another process (same user or root only). |
Edge cases: the link target depends on what the stream is: a terminal such as /dev/pts/0, a pipe like pipe:[12345], or a regular file. Opening /dev/stdout when it is a file may reopen it from the start (truncating it), unlike >&1, which shares the position.
Usage notes
- Prefer
-when supported. Many tools treat-as stdin or stdout (cat -,tar -f -). Use/dev/stdinonly when a program demands a path. - Shell redirection is simpler.
echo msg >&2writes to stderr without opening any file, and works even where/devis odd. - Here the shell does the work. In Bash,
<(cmd)and>(cmd)create/dev/fd/Nnames for pipes; they are not POSIX, sodashscripts cannot use them. - Sudo and cron: inside
sudo sh -c,/dev/stdoutrefers to the new process's output, which is usually what you want. Under cron there may be no terminal at all. - Not every stream can be reopened. Opening
/dev/stdoutre-opens whatever it points to; for a pipe or socket that may fail (this sandbox refuses it), so plain>&1is the safer way to refer to the same stream. - Debugging open files:
ls -l /proc/$$/fdlists the descriptors of the current shell.