Skip to content

Instantly share code, notes, and snippets.

@jaggzh
Last active January 15, 2022 10:10
Show Gist options
  • Save jaggzh/4edda5004fd922d1390e79dbad9d87ac to your computer and use it in GitHub Desktop.
Save jaggzh/4edda5004fd922d1390e79dbad9d87ac to your computer and use it in GitHub Desktop.
Bash function for a prompt with visible seconds countdown
# See video: https://youtu.be/5z0JImIiQVs
readcountdown1 is a function
readcountdown1 ()
{
local secs="$1";
local var="$2";
local msg="$3";
local uinp;
for ((i=secs; i>0; i--))
do
unset "$var";
if ! read -r -n 1 -t 1 -p "$msg (${i}s): " uinp; then
echo -ne '\r';
else
if [[ "$uinp" != '' ]]; then
echo;
fi;
eval "export $var='$uinp'";
return 0;
fi;
done;
if [[ "$i" -lt 1 ]]; then
echo;
fi;
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment