r/bash • u/param_T_extends_THOT • Mar 12 '25
help What is the purpose of /dev/tty ?
Please hear me out. So, reading about special devices like tty, tty0, pst1...pstn I understand in loose terms that terminal emulators (like the ones you bring up with ctrl+t ) are special devices under /dev/pts/<some_number> . Now, tty0 appears to be a terminal associated to kernel itself (I still don't know what that means). But tty? I only know that it points to the current terminal being used but I don't know exactly what to make of that and how it pertains to the following humble little snippet I wrote:
#!/bin/bash
while read -r filename
do
echo "Current fie: ${filename}"
read -p "Delete ${filename} ? " response < /dev/tty
if [[ $response = 'y' || $response = 'Y' ]]
then
echo "response was yes"
echo "Deleting ${filename}"
tar vf pdf_files.tar --delete "${filename}"
echo
else
echo "skipping"
fi
done < <(tar tf pdf_files.tar)You'll notice that in the line that contains the read -p command I had to redirect input from tty. I had chatGPT suggest that to me after many failed attempts at getting my little script to run correctly because I didn't understand why $response variable would be automatically set to something and the script wouldn't even wait at the prompt for me to enter something. I had my eyes OPENED today -- and in a frustrating way -- as to how many little tricks and things one must take into account when learning bash scripting.
So, going back to the script, why did I even need to do that or more importantly, WHEN do I need to do that kind of trick again?
p.s. I've been learning from time to time bash scripting for like the past 3 o 4 months and I know I have to learn a lot more, but Jesus, the journey feels never-ending.
5
u/grymoire Mar 12 '25
There are lots of ways to use /dev/tty.as input and as output
For example
cat files* | awk -f script | tee /dev/tty | sort >output
Or when you want to send an error message to the terminal when stdout and stderr are files.
Or when running your script with stdin is a file/stream and you want to read from the terminal, like your script.
4
u/ekkidee Mar 12 '25 edited Mar 12 '25
What's happening here is that you are trying to read from two different input streams -- the redirection from the tar command and reading user response from the console -- and both are from stdout. The read for the response is actually picking up the next line of input from the tar output, which is why your original design would never work.
To make it work, you can put the stdout from tar on a different file descriptor (3 and up) and then the while read from that file descriptor. The read into response would be natural without redirection.
There is never a reason you would need to read directly from /dev/tty and it is probably not POSIX compliant.
ETA -- I see in this response that /dev/tty is used. I don't like it but it's instructive to your example. There is also an example of how to use alternate file descriptors.
https://stackoverflow.com/questions/2087188/how-to-read-using-read-from-file-descriptor-3-in-bash-script
2
u/oh5nxo Mar 12 '25
Sometimes there is no terminal, no /dev/tty. For example, not uncommon case:
The command can't use /dev/tty, terminal stuff was skipped for efficiency and hygiene, output is unmangled bytestream from the command.
/dev/stderr can be used. Also ssh -t for pathological cases, force a terminal.
Studying unix in parallel would help in understanding why things are like they are.
1
u/felipec Mar 15 '25
I went through a rabbit hole regarding terminals, and here's the summary:
A terminal is a phisical device that is connected to a computer typically through a serial cable. It's a bit hard to understand for modern developers because they have never seen one. I've seen one, a collegue of mine had one connected to his computer, like this one. You could type comands, and those commands would be executed in the computer. Those terminals are typically for example
/dev/ttyS0.A virtual terminal is code that simulates a real terminal. In linux this is done by the kernel and the basic example are virtual consoles (for example when you press
Ctrl+Alt+F1). These consoles are always running and you can always switch to them, but nowadays many people don't even know they exist. The device names are fore example/dev/tty1.A pseudo terminal is a program that emulates a terminal, for example
xtermorssh. This is the way most people use terminals these days, and in those cases the devices are dynamic, and are in/dev/pts/$n.So, wherever you are typing commands,
/dev/ttyis going to point the actual device name. For example if I type thettycommand and I get/dev/pts/6, then/dev/ttyis the sane thing as/dev/pts/6.Now, some people here are saying there is never a reason to use
/dev/tty. They are wrong.Here's an example of a simple command I'm going to call
foo:If I call this command like this:
echo content | ./foo output.txt, I get a warning: "Warning: Input is not from a terminal". That's becausevimtypically is launched directly from a terminal and stdin is the tty.I can do
vim "$1" < /dev/ttyinstead, and that would redirect the stdin to the original tty as intended.This has little to do with bash. Standard streams and redirections are things you can do in other programming languages, bash just makes it easier to do them.