Answers:
男子猛击
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.
man bash | wc
指示[GNU bash版本3.2.57]手册页为4890行,37094个字,329778个字符。该答案仅将相关的7行,176个单词,1115个字符剥离。我认为这个答案值得您的支持。(以及该评论是否;-)
从Doc处:https : //tiswww.case.edu/php/chet/bash/bashref.html#Shell-Arithmetic
前导0的常量被解释为八进制数。前导“ 0x”或“ 0X”表示十六进制。否则,数字采用[base#] n的形式,其中可选的底数是2到64之间的十进制数,代表算术底数,n是该底数中的数字。如果省略了base#,则使用以10为基数。当指定n时,大于9的数字依次由小写字母,大写字母“ @”和“ _”表示。如果base小于或等于36,则小写字母和大写字母可以互换使用,以表示10到35之间的数字。
所以echo $((16#FF))
输出255
和echo $((2#0110))
输出6
Ipor的回答很好,但略微不完整。bash手册页中带引号的部分指出,该语法仅适用于常量,而不是常量。你应该问如何真的有效![base#]n
2#$1
扩张
拆分成单词后,在命令行上执行扩展。执行了七种扩展:大括号扩展,代字号扩展,参数和变量扩展,命令替换,算术扩展,单词拆分和路径名扩展。
扩展顺序为:大括号扩展;波浪号扩展,参数和变量扩展,算术扩展和命令替换(以从左到右的方式完成);分词 和路径名扩展。
基本上,Bash首先进行变量替换,因此$1
首先将其替换为它的值。只有这样,它才能进行算术扩展,只能看到一个适当的常数。
$1
是输入。”
$1
在对算术表达式求值之前,将参数扩展为生成整数常量。请参阅gnu.org/software/bash/manual/bash.txt,第3.5节”