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
0
are interpreted as octal numbers. A leading0x
or0X
denotes hexadecimal. Otherwise, numbers take the form[base#]n
, wherebase
is a decimal number between2
and64
representing the arithmetic base andn
is a number in thatbase
. Ifbase#
is omitted, then base10
is used. The digits greater than9
are represented by the lowercase letters, the uppercase letters,@
, and_
, in that order. Ifbase
is 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