Skip to content

Instantly share code, notes, and snippets.

@opensiriusfox
Last active June 10, 2021 15:00
Show Gist options
  • Save opensiriusfox/d29aecd8b265c53f2fa60ffc9358e420 to your computer and use it in GitHub Desktop.
Save opensiriusfox/d29aecd8b265c53f2fa60ffc9358e420 to your computer and use it in GitHub Desktop.
A script to reproduce a regression from Bash 4.4 to 5.0
#!/bin/bash
echo "====="
echo "== Test #1"
echo "== Working in bash 4, broken in bash 5"
echo "====="
# Test Case 1
startTime=$(date +"%s")
recheckSleepTime=0.2
for X in $(seq 20); do
timeDelta=$(($(date +"%s")-$startTime))
sleepTime="$(date --date=@$timeDelta -u +"%H:%M:%S")"
# Starting in bash 5.0 releases, the inclusion of the "columns" variable here or
# a call to $(tput cols) will result in the printf using \r\n rather than \r
printf "\r\033[12G%-${COLUMNS}s" "running... ($sleepTime)"
sleep $recheckSleepTime
done
echo ""
echo "====="
echo "== Test #2"
echo "== Broken in both bash 4 and 5"
echo "====="
## Test Case 2
startTime=$(date +"%s")
recheckSleepTime=0.2
for X in $(seq 20); do
timeDelta=$(($(date +"%s")-$startTime))
sleepTime="$(date --date=@$timeDelta -u +"%H:%M:%S")"
# Starting in bash 5.0 releases, the inclusion of the "columns" variable here or
# a call to $(tput cols) will result in the printf using \r\n rather than \r
printf "\r\033[12G%-$(tput cols)s" "running... ($sleepTime)"
sleep $recheckSleepTime
done
## Test Case 3
echo ""
echo "====="
echo "== Test #3"
echo "== Working in both bash 4 and 5"
echo "====="
startTime=$(date +"%s")
recheckSleepTime=0.2
for X in $(seq 20); do
timeDelta=$(($(date +"%s")-$startTime))
sleepTime="$(date --date=@$timeDelta -u +"%H:%M:%S")"
# Starting in bash 5.0 releases, the inclusion of the "columns" variable here or
# a call to $(tput cols) will result in the printf using \r\n rather than \r
printf "\r\033[12G%-60s" "running... ($sleepTime)"
sleep $recheckSleepTime
done
echo ""
echo "DONE!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment