I have a set of records to loop. The numbers range from 0000001 to 0089543 that ill call UIDX.
if i try something like:
for ((i=0; i< 0089543; i++)); do
((UIDX++))
done
counter increments 1, 2, 3, 4 as opposed to the 0000001, 0000002... that i need.
what is the best way to pad those leading zero's?
Similar to other programming language bash also supports increment and decrement operators. The increment operator ++ increases the value of a variable by one. Similarly, the decrement operator -- decreases the value of a variable by one.
So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage: name A word consisting only of alphanumeric characters and under- scores, and beginning with an alphabetic character or an under- score.
The operators can be used before or after the operand. They are also known as: prefix increment: ++i. prefix decrement: --i. postfix increment: i++
The “[ -f ~/. bashrc]” is known as a test command in bash. This command, which includes the “-f” expression, will check if the ~/. bashrc file exists (i.e. the file .
Use the printf
command to format the numbers with leading zeroes, eg:
for ((i = 0; i < 99; ++i)); do printf -v num '%07d' $i; echo $num; done
From man bash
:
printf [-v var] format [arguments]
Write the formatted arguments to the standard output under the control of the format. The -v option causes the output to be assigned to the variable var rather than being printed to the standard output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With