O que significa ((num # Hour)) em um script bash?

17

Eu tenho um script bash contendo as seguintes 2 linhas:

Hour=$(date +"%H")
Hour=$((10#$Hour))

O que a linha 2 faz?

    
por Mohsen Abasi 07.04.2018 / 16:06

1 resposta

23

O 10# diz para expandir o número usando a base 10:

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 the optional 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. When specifying n, 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.

Exemplos:

Base 16:

$ echo $((16#A))
10

Base 8:

$ echo $((8#12))
10

Base 2:

$ echo $((2#1010))
10

Como a steeldriver aponta, isso provavelmente está sendo feito para lidar com possíveis zeros iniciais do comando date, mas com versões recentes da data do GNU, isso pode ser feito mais facilmente usando: date +%-H

    
por 07.04.2018 / 16:13