help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Exiting the "read" builtin with trapped ^C


From: Stan Marsh
Subject: Exiting the "read" builtin with trapped ^C
Date: Mon, 22 Jun 2026 08:06:24 -0600

Consider the following script:

    #!/bin/bash
    set -o vi
    trap 'do_TIO 10;echo "In trap, REPLY = $REPLY";continue' INT
    for i in a b c; do
        read -ei "Default value: $i" -p "Enter something: "
        echo "REPLY = $REPLY"
    done

The issue is that without the invocation of do_TIO (which will be
described later), hitting ^C in the 'read' does not exit the read;
you still have to hit enter to get out of it. I assume this happens
because 'read' ignores ^C for the duration of the invocation of
'readline' and so on.

'do_TIO' is a small C program that pushes keystrokes using TIOCSTI. In
this case, it pushes an LF into the keyboard buffer, and this causes
the 'read' to exit when we hit ^C. So, the above script does work as
written, but obviously, this approach is less than ideal.

Note that similar things happen with QUIT. In general, I'd like a good
way to have 'read' exit sensibly when I hit ^C (or ^\).

Is there a better way?

=================================================================================
Please do not send me replies to my posts on the list.
I always read the replies via the web archive, so CC'ing to me is unnecessary.

When responding to my posts, please try to refrain from giving bureaucratic
answers.  If you have nothing useful to say, then just click Next and go on.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]