I am confused about the following code snippet:
#!/bin/bash
H=$(date +%H);
if (( 10#$H > 5 ))
then
# do something
else
# do something else
fi
What does the (( 10#$H > 5 )) mean in above code snippet?
The 10#$H means to expand the number using base 10.
This is probably done to remove any leading zeros from the date due to the fact that bash will interpret the number in base 8 (octal).
Example:
$ echo "$(( 08 < 5 ))"
bash: 08: value too great for base (error token is "08")
ARITHMETIC EVALUATION: Constants with a leading
0are interpreted as octal numbers. A leading0xor0Xdenotes hexadecimal. Otherwise, numbers take the form[base#]n, wherebaseis a decimal number between2and64representing the arithmetic base andnis a number in thatbase. Ifbase#is omitted, then base10is used. The digits greater than9are represented by the lowercase letters, the uppercase letters,@, and_, in that order. Ifbaseis less than or equal to36, lowercase and uppercase letters may be used interchangeably to represent numbers between 10 and 35.source:
man bash
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