With read -n "$n"
(not a POSIX feature), and if stdin is a terminal device, read
puts the terminal out of the icanon
mode, as otherwise read
would only see full lines as returned by the terminal line discipline internal line editor and then reads one byte at a time until $n
characters or a newline have been read (you may see unexpected results if invalid characters are entered).
It reads up to $n
character from one line. You'll also need to empty $IFS
for it not to strip IFS characters from the input.
Since we leave the icanon
mode, ^D
is no longer special. So if you press Ctrl+D, the ^D
character will be read.
You wouldn't see eof from the terminal device unless the terminal is somehow disconnected. If stdin is another type of file, you may see eof (like in : | IFS= read -rn 1; echo "$?"
where stdin is an empty pipe, or with redirecting stdin from /dev/null
)
read
will return 0 if $n
characters (bytes not forming part of valid characters being counted as 1 character) or a full line have been read.
So, in the special case of only one character being requested:
if IFS= read -rn 1 var; then
if [ "${#var}" -eq 0 ]; then
echo an empty line was read
else
printf %s "${#var} character "
(export LC_ALL=C; printf '%s\n' "made of ${#var} byte(s) was read")
fi
else
echo "EOF found"
fi
Doing it POSIXly is rather complicated.
That would be something like (assuming an ASCII-based (as opposed to EBCDIC for instance) system):
readk() {
REPLY= ret=1
if [ -t 0 ]; then
saved_settings=$(stty -g)
stty -icanon min 1 time 0 icrnl
fi
while true; do
code=$(dd bs=1 count=1 2> /dev/null | od -An -vto1 | tr -cd 0-7)
[ -n "$code" ] || break
case $code in
000 | 012) ret=0; break;; # can't store NUL in variable anyway
(*) REPLY=$REPLY$(printf "\\$code");;
esac
if expr " $REPLY" : ' .' > /dev/null; then
ret=0
break
fi
done
if [ -t 0 ]; then
stty "$saved_settings"
fi
return "$ret"
}
Note that we return only when a full character has been read. If the input is in the wrong encoding (different from the locale's encoding), for instance if your terminal sends é
encoded in iso8859-1 (0xe9) when we expect UTF-8 (0xc3 0xa9), then you may enter as many é
as you like, the function will not return. bash
's read -n1
would return upon the second 0xe9 (and store both in the variable) which is a slightly better behaviour.
If you also wanted to read a ^C
character upon Ctrl+C (instead of letting it kill your script; also for ^Z
, ^\
...), or
^S
/^Q
upon Ctrl+S/Q (instead of flow control), you could add a -isig -ixon
to the stty
line. Note that bash
's read -n1
doesn't do it either (it even restores isig
if it was off).
That will not restore the tty settings if the script is killed (like if you press Ctrl+C. You could add a trap
, but that would potentially override other trap
s in the script.
You could also use zsh
instead of bash
, where read -k
(which predates ksh93
or bash
's read -n/-N
) reads one character from the terminal and handles ^D
by itself (returns non-zero if that character is entered) and doesn't treat newline specially.
if read -k k; then
printf '1 character entered: %q\n' $k
fi
First, the entire thing prints only after the 5 second sleep so from that I deduced the output from the kernel to the terminal is line buffered.
No, the output from your program to the kernel is line-buffered. That's the default behaviour for stdio
when stdout
is a terminal. Add the call setbuf(stdout, NULL)
to turn output buffering off for stdout
. See setbuf(3)
.
- Since the
\b\b
goes back two spaces, to the position of l
then similar to how l
was replaced by h
, the o
should have been replaced by \n
. Why wasn't it?
Because the newline character just moves the cursor (and scrolls the screen), it doesn't print as a visible character that would take up the place of a glyph on the terminal. If we assume it would take the place of a glyph, what would it look like?
- if I input something into the very same program, and press Backspace, it erases the last character, but not for the output. Why?
Well, what happens when you type depends on what mode the terminal is in. Roughly, it can be in the usual "cooked" mode where the terminal itself provides elementary line editing (handles backspaces); or in a "raw" mode, where all keypresses go to the application, and it's up to the application to decide what to do with them, and what to output in response. Cooked mode usually goes along with "local echo" where the terminal (local to the user) prints out the characters as they are typed. In raw mode, the application usually takes care of echoing the typed characters, to have full control over what's visible.
See e.g. this question for discussion on the terminal modes:
What’s the difference between a “raw” and a “cooked” device driver?
If you run e.g. cat
, the terminal will be in cooked mode (the default) and handle the line editing. Hitting for example xBackspaceCtrl-D will result in cat
just reading the empty input, signalling the end of input . You can check this with strace
. But if you run an interactive Bash shell instead, it will handle the backspace by itself, and output what it considers appropriate to do what the user expects, i.e. wipe one character.
Here's part of the output for strace -etrace=read,write -obash.trace bash
, after entering the mentioned sequence xBackspaceCtrl-D:
read(0, "x", 1) = 1
write(2, "x", 1) = 1
read(0, "\177", 1) = 1
write(2, "\10\33[K", 4) = 4
read(0, "\4", 1) = 1
First, Bash read
s and write
s the x
, outputting it to the terminal. Then it reads the backspace (character code 0177 in octal or 127 in decimal), and outputs the backspace character (octal 010, decimal 8(*)) which moves the cursor back and outputs the control sequence for clearing the end of the line, <ESC>[K
. The last \4
is the Ctrl-D
, which is used by Bash to exit the program.
(* in input, Ctrl-H
would have the decimal character code 8. Backspace is either the same or 127 as here, depending again on how the terminal is set up.)
In comparison, the same experiment with cat
only shows a single read of zero bytes, the "end of file" condition. End of file can mean either a connected pipe or socket being closed, an actual end of file, or Ctrl-D
being received from a terminal in cooked mode:
read(0, "", 131072) = 0
In particular, cat
doesn't see the x
, nor the backspace, nor the actual code for Ctrl-D
: they're are handled by the terminal. Which might be the virtual terminal driver in the kernel; an actual physical terminal over a serial connection or such; or a terminal emulator like xterm either running on the same machine or at the remote end of an SSH connection. It doesn't matter for the userspace software.
Best Answer
backspace is only moving the cursor backward.
backspace (or delete or whatever character depending on the configuration) deletes the last printed character only when these conditions are met:
echo
in your exampleIf you want to erase the c in your example, you need to overwrite it by another character, for example: