Skip to content

Instantly share code, notes, and snippets.

@opplatek
Created September 28, 2023 08:22
Show Gist options
  • Save opplatek/c77daef31b0bfec33aa9254c89559e24 to your computer and use it in GitHub Desktop.
Save opplatek/c77daef31b0bfec33aa9254c89559e24 to your computer and use it in GitHub Desktop.
Fill (prefix zeros) number of digits to predefined number
#!/bin/bash
DIGIT=52
TARGET_DIGITS=4
# Easier
echo $(printf "%0${TARGET_DIGITS}d" ${DIGIT})
#0052
# More complicated
FILL=$(echo $TARGET_DIGITS - ${#DIGIT} | bc)
echo $(printf '0%.0s' $(seq 1 "$FILL"))${DIGIT}
#0052
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment