help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Why does ctrl-c behave differently for builtin sleep in


From: Peng Yu
Subject: Re: [Help-bash] Why does ctrl-c behave differently for builtin sleep in a subshell?
Date: Mon, 17 Dec 2018 13:57:01 -0600

Note that when SIGINT is set, whether an external or built-in sleep is
used will not result in any difference.

If I use some other method (say a for loop) instead of built-in sleep
to introduce a delay, the EXIT can be reached upon ctrl-c. So this
sounds like that the builtin sleep somehow causes the problem. Is it
the case?

$ cat main_builtin_INT.sh
#!/usr/bin/env bash
trap 'echo at EXIT' EXIT
trap 'echo at SIGINT' SIGINT
enable -f ~/Downloads/bash-4.4/examples/loadables/sleep sleep
sleep 10
$ cat  \(/main_builtin_INT.sh
#!/usr/bin/env bash
(
    trap 'echo at EXIT' EXIT
    enable -f ~/Downloads/bash-4.4/examples/loadables/sleep sleep
    sleep 10
)
$ ./main_builtin_INT.sh
^Cat SIGINT
at EXIT
$ '('/main_builtin_INT.sh
^Cat SIGINT
at EXIT

$ cat ./main_builtin_for.sh
#!/usr/bin/env bash
(
    trap 'echo at EXIT' EXIT
    for ((i=0;i<100000;++i)); do
        :
    done
)
$  ./main_builtin_for.sh
^Cat EXIT

On Mon, Dec 17, 2018 at 1:41 PM Peng Yu <address@hidden> wrote:
>
> Hi,
>
> See the follow code, '(/main_builtin.sh' behaves differently from the
> other three scripts upon receiving ctrl-c. This is counter-intuitive.
> I'd expect whether a built-in command called the result should be the
> same.
>
> How to understand why there is such a difference? Is there an easy
> workaround to make the result the same as the other three?
>
> $   ./main.sh
> ^Cat EXIT
>
> $   ./main_builtin.sh
> ^Cat EXIT
>
> $ \(/main.sh
> ^Cat EXIT
>
> $ \(/main_builtin.sh
> ^C
>
> $ cat main.sh
> #!/usr/bin/env bash
> trap 'echo at EXIT' EXIT
> sleep 10
>
> $ cat main_builtin.sh
> #!/usr/bin/env bash
> trap 'echo at EXIT' EXIT
> enable -f ~/Downloads/bash-4.4/examples/loadables/sleep sleep
> sleep 10
>
> $ cat '(/main.sh'
> #!/usr/bin/env bash
> (
>     trap 'echo at EXIT' EXIT
>     sleep 10
> )
>
> $ cat '(/main_builtin.sh'
> #!/usr/bin/env bash
> (
>     trap 'echo at EXIT' EXIT
>     enable -f ~/Downloads/bash-4.4/examples/loadables/sleep sleep
>     sleep 10
> )
>
> --
> Regards,
> Peng



-- 
Regards,
Peng



reply via email to

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