Zero padding in BASH shell:
There are a couple of ways to pad integers with leading zeros in BASH. One way is to add a larger number and truncate the leading digit:
foo=100000
smallNumber=25
largeNumber=$(( $foo + $smallNumber ))
zeroPaddedNumber=${largeNumber:1}
echo $zeroPaddedNumber

This displays: “00025″ the ${largeNumber:1} chops off the first character from 100025.

Another way is to use printf, but this creates another process and is more expensive in larger loops.
zeroPaddedNumber=`printf "%05d" $smallNumber`

Same results, but in larger loops will have slight performance penalty

Enforcing decimal for integers with leading zeros
Doing calculations with zero padded integers may provide inaccurate results. Specifically “00025″ is counted as octal in bash. To force bash to recognize it as a decimal number:

smallNumber='00025'
decimalNumber="10#"$smallNumber
hexNumber="0x"$smallNumber
echo $(( $smallNumber + 1))
echo $(( $decimalNumber + 1))
echo $(( $hexNumber + 1))

The above prints “22″, “26″, and “38″. The “$(( ))” construct does arithmetic calculation and returns the value in base 10.

String substitution or substring search in bash variables
Sometimes you assign the output of a command to a variable, expecting it to hold some information. If the command failed or did return the expected values, you must check the values returned.

mylongstring="This is a really long string I want to check"
searchstring="I want"
if [[ ${mylongstring#*$searchstring*} != $mylongstring ]]; then
echo "'$searchstring' was found in '$mylongstring'"
fi
searchstring="I have"
if [[ ${mylongstring#*$searchstring*} == $mylongstring ]]; then
echo "'$searchstring' was not found in '$mylongstring'"
fi