It is possible to time out the read command using coroutines.
We need a helper script, let's call it timout_read.sh, and main script with read command.
1. timeout_read.sh
#! /usr/bin/ksh
typeset -i timeout_after_this=$1
sleep $timeout_after_this
kill -0 $PPID 2>/dev/null && kill -USR1 $PPID
#ps -p $PPID >/dev/null && kill -USR1 $PPID 2>/dev/null
2. main.sh
#! /usr/bin/ksh* read in bash has -t option for this functionality.
myvar=.yes ## default value.
timeout=${1:-4}
read_myvar()
{
trap return USR1
timeout_read.sh $1 &
read myvar?"Your value for myvar: "
trap "" USR1
}
## do something
read_myvar $timeout
if [[ $myvar = .yes ]]; then
# do default action
echo Read timed out. myvar = $myvar
else
# do proper action depending on user input
echo You entered $myvar
fi