Skip to content

Instantly share code, notes, and snippets.

@bkw777
Last active August 6, 2021 22:02
Show Gist options
  • Save bkw777/449c354b54827da1bf3fb1d60a2b2fb0 to your computer and use it in GitHub Desktop.
Save bkw777/449c354b54827da1bf3fb1d60a2b2fb0 to your computer and use it in GitHub Desktop.
sleep in pure native bash without /usr/bin/sleep and without even subshell
#!/usr/bin/env bash
# sleep without externals or children
# and without the bash sleep builtin
# mkfifo is not exactly free, but you only do this part once.
# After that it is relatively free to call _sleep all you want even in tight loops.
sleep_fifo=/tmp/.${0//\//_}.$$.sleep.fifo
trap "rm -f $sleep_fifo" EXIT
mkfifo $sleep_fifo || { echo "$0: Error creating sleep fifo \"$sleep_fifo\"" >&2 ; exit 1 ; }
exec 9<>$sleep_fifo
_sleep () {
local x
read -t ${1:-1} -u 9 x
}
####################################################################
# main
for ((i=0;i<50;i++)) {
echo "$i: sleeping 0.1 seconds"
_sleep 0.1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment