How to exit from infinite loop in parent terminal?

1

To test trap command I created the following loop in terminal:

while true ; do echo BEGIN ; bash -e ; echo "exit code = $?" ; echo END ; done

How to exit this loop and turn back to the parent shell?

In case of using remote shell (ssh) instead of bash there is a delay during connection establishing process when I can press Ctrl+C. But in the above case I can't be fast enough to catch time window.

Tomilov Anatoliy

Posted 2018-01-19T07:08:49.583

Reputation: 251

Answers

2

In this case exiting the child shell causes another child shell to appear immediately. To break the loop execute this from within the child shell:

kill -s SIGINT $PPID

Then exit the child shell (exit or Ctrl+D). $PPID ensures the signal gets to the parent shell (without playing with ps and guessing).

Kamil Maciorowski

Posted 2018-01-19T07:08:49.583

Reputation: 38 429

SIGINT is Ctrl+C? – Tomilov Anatoliy – 2018-01-19T11:35:34.880

@Orient. Yes. See this question.

– Kamil Maciorowski – 2018-01-19T11:58:20.957