Skip to main content
work in progress
Source Link
meuh
  • 54.7k
  • 2
  • 70
  • 138

Actually, interrupting bash's internal read seems to be a bit different to interrupting a command run by bash. Normally, when you enter trap, $? is set and you can preserve it and exit with the same value:

trap 'rc=$?; echo $rc SIGINT; exit $rc' INT
trap 'rc=$?; echo $rc EXIT; exit $rc' EXIT

If your script is interrupted when executing a command like sleep or even a builtin like wait, you will see

130 SIGINT
130 EXIT

and the exit code is 130. However, for read -p, it seems $? is 0 (on my version of bash 4.3.42 anyway).


The handling of signals during read might be work in progress, according to the changes file in my release... (/usr/share/doc/bash/CHANGES)

changes between this version, bash-4.3-alpha, and the previous version, bash-4.2-release.

  1. New Features in Bash

r. When in Posix mode, `read' is interruptible by a trapped signal. After running the trap handler, read returns 128+signal and throws away any partially-read input.

Actually, interrupting bash's internal read seems to be a bit different to interrupting a command run by bash. Normally, when you enter trap, $? is set and you can preserve it and exit with the same value:

trap 'rc=$?; echo $rc SIGINT; exit $rc' INT
trap 'rc=$?; echo $rc EXIT; exit $rc' EXIT

If your script is interrupted when executing a command like sleep or even a builtin like wait, you will see

130 SIGINT
130 EXIT

and the exit code is 130. However, for read -p, it seems $? is 0 (on my version of bash 4.3.42 anyway).

Actually, interrupting bash's internal read seems to be a bit different to interrupting a command run by bash. Normally, when you enter trap, $? is set and you can preserve it and exit with the same value:

trap 'rc=$?; echo $rc SIGINT; exit $rc' INT
trap 'rc=$?; echo $rc EXIT; exit $rc' EXIT

If your script is interrupted when executing a command like sleep or even a builtin like wait, you will see

130 SIGINT
130 EXIT

and the exit code is 130. However, for read -p, it seems $? is 0 (on my version of bash 4.3.42 anyway).


The handling of signals during read might be work in progress, according to the changes file in my release... (/usr/share/doc/bash/CHANGES)

changes between this version, bash-4.3-alpha, and the previous version, bash-4.2-release.

  1. New Features in Bash

r. When in Posix mode, `read' is interruptible by a trapped signal. After running the trap handler, read returns 128+signal and throws away any partially-read input.

Source Link
meuh
  • 54.7k
  • 2
  • 70
  • 138

Actually, interrupting bash's internal read seems to be a bit different to interrupting a command run by bash. Normally, when you enter trap, $? is set and you can preserve it and exit with the same value:

trap 'rc=$?; echo $rc SIGINT; exit $rc' INT
trap 'rc=$?; echo $rc EXIT; exit $rc' EXIT

If your script is interrupted when executing a command like sleep or even a builtin like wait, you will see

130 SIGINT
130 EXIT

and the exit code is 130. However, for read -p, it seems $? is 0 (on my version of bash 4.3.42 anyway).