#!/bin/bash

#C-c won't break the builtin read unless default SIGINT trap is restored, thus needing two C-c to exit

interrupted() {
	local ec="$?"
	trap - SIGINT  #this restores prev. behaviour, so now another C-c will stop 'read -rp'
  echo "interrupted sees exit code '$ec'"
#  exit "$ec"  #this is needed ONLY for when 'sleep' is used and interrupted, or else it will continue running and hit 'Normal exit', if used with 'read' then it will exit without breaking 'read' thus exit code is 0 instead of 130 (128+2 aka SIGINT==2)
}

trap interrupted SIGINT
builtin read -rp "Press C-c here..."  #can use 'sleep 100' instead of this 'read...' here, for testing how the behaviour is different with an external command instead of a builtin one.
#sleep 100
#ping 127.0.0.1  #ping will ec=0 on C-c
ec="$?"
echo
echo "Normal exit sees ec=$ec"
exit "$ec"
