1

Example:

cat < test.txt

  1. Is the content of the file test.txt written/passed to stdin of the cat, and then the cat reads its stdin?

OR

  1. Does the file test.txt itself become the stdin of the cat? In other words, is the stdin of the cat changed to test.txt by setting the file descriptor (fd) of the text file to 0?

1 Answer 1

1

Option number 2: test.txt is opened, and cat is set up with its standard input pointing to that file.

On Linux, you can see this by running

$ touch /tmp/foo
$ sleep 120 < /tmp/foo &
[1] 3006118
$ ls -l /proc/3006118/fd
total 0
lr-x------ 1 steve steve 64 May  4 16:11 0 -> /tmp/foo
lrwx------ 1 steve steve 64 May  4 16:11 1 -> /dev/pts/3
lrwx------ 1 steve steve 64 May  4 16:11 2 -> /dev/pts/3

The process’ standard input is /tmp/foo directly.

2
  • So, the fd of stdin can be anything other than just 0?
    – jackmiller
    May 4 at 14:16
  • 1
    Oh, my answer might be confusing on that front — “given the resulting file descriptor as its standard input” means that whatever sets up the redirection (the shell, in this case) duplicates the file descriptor so that it’s 0 in the process which will end up running cat. May 4 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.