Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the '(( 10#$H > 5 ))' mean in bash script?

Tags:

linux

bash

shell

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?

like image 738
Ren Avatar asked Dec 14 '22 13:12

Ren


1 Answers

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 leading 0x or 0X denotes hexadecimal. Otherwise, numbers take the form [base#]n, where base is a decimal number between 2 and 64 representing the arithmetic base and n is a number in that base. If base# is omitted, then base 10 is used. The digits greater than 9 are represented by the lowercase letters, the uppercase letters, @, and _, in that order. If base is less than or equal to 36, lowercase and uppercase letters may be used interchangeably to represent numbers between 10 and 35.

source: man bash

like image 139
tomerpacific Avatar answered Dec 17 '22 22:12

tomerpacific