Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

75%
+4 −0
Q&A How do programs like less receive keyboard input as well as from a pipe?

As you've observed, programs like less don't rely on STDIN for interactive input. Instead, they look for their controlling terminal and open a new file descriptor reading from that. On most syste...

posted 4mo ago by gnoutchd‭

Answer
#1: Initial revision by user avatar gnoutchd‭ · 2026-05-14T17:08:28Z (4 months ago)
As you've observed, programs like `less` don't rely on STDIN for interactive input.  Instead, they look for their *controlling terminal* and open a new file descriptor reading from that.  On most systems, that's achieved by opening `/dev/tty` for reading. Much as with STDIN/STDOUT/STDERR, a Unix process usually inherits its controlling terminal from its parent, in this case the shell.  See also [tty(4)](https://manpages.debian.org/stable/manpages/tty.4.en.html) and [credentials(7)](https://manpages.debian.org/stable/manpages/credentials.7.en.html).

Another program that does this is `sudo`, which is why you can type things like:

```
echo foo | sudo tee /root/bar
```


and it'll work even if `sudo` wants a password.  What's more, it uses the controlling terminal for printing the password prompt too, so you could even do:

```
cut -d: -f1 /etc/passwd | sudo xargs getent shadow | less
```

and it still works.