O que o echo $ ((2 # $ 1)) faz exatamente?

53

O seguinte script bash exibe um número decimal quando determinado número binário.

echo $((2#$1))

Por que exatamente?

Eu entendo que $1 é a entrada. Talvez 2 seja a base (binária). Mas não consigo entender a sintaxe usada.

    
por NanoPish 02.01.2017 / 14:08

3 respostas

74

man bash

   echo [-neE] [arg ...]
          Output  the  args,  separated  by spaces, followed by a newline.
          The return status is 0 unless a write error occurs.   If  -n  is
          specified, the trailing newline is suppressed.  If the -e option
          is given,  interpretation  of  the  following  backslash-escaped
          characters  is  enabled.

[...]

   Arithmetic Expansion
       Arithmetic  expansion allows the evaluation of an arithmetic expression
       and the substitution of the result.  The format for  arithmetic  expan‐
       sion is:

              $((expression))

[...]

   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  repre‐
   sent numbers between 10 and 35.
    
por 02.01.2017 / 14:16
30

Do documento em: link

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.

Portanto, echo $((16#FF)) outputs 255 e echo $((2#0110)) outputs 6

    
por 02.01.2017 / 14:21
25

A resposta do Ipor é excelente, mas muito ligeiramente incompleta. A parte citada da página man bash afirma que a sintaxe [base#]n funciona apenas para constantes e 2#$1 não é uma constante. Você deveria estar perguntando como isso realmente funciona !

EXPANSION

    Expansion is performed on the command line after it has been split into words.  There are seven kinds of expansion performed: brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, word splitting, and pathname expansion.

    The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and pathname expansion.

Basicamente, o Bash está fazendo a substituição de variáveis primeiro, de modo que o $1 seja primeiro substituído pelo seu valor. Só então faz a expansão aritmética, que vê apenas uma constante adequada.

    
por 03.01.2017 / 11:48

Tags