help-bash
[Top][All Lists]
Advanced

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

Re: Exiting the "read" builtin with trapped ^C


From: Chet Ramey
Subject: Re: Exiting the "read" builtin with trapped ^C
Date: Fri, 26 Jun 2026 15:27:56 -0400
User-agent: Mozilla Thunderbird

On 6/22/26 10:06 AM, Stan Marsh wrote:
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.

Not exactly. Trapping SIGINT indicates that you want control -- the
default behavior of interrupting the read doesn't happen. It doesn't
really `ignore' the SIGINT -- the shell still tries to execute the
trap command on every ^C -- but the `read' invocation will run to
completion. The trap command doesn't seem to `work' after executing
`continue' once, because that will only be in effect once the `read'
finishes. It's not the equivalent of `longjmp', nor does it cause the
`read' to complete.

Different shells handle this differently: some behave as you like,
some catch the ^C and run the trap when the `read' builtin completes
after having read something, some behave like bash.


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 ^\).

Sure -- don't trap SIGINT. It doesn't mean what you think it does.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


reply via email to

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