|
发表于 2008-5-26 19:12:44
|
显示全部楼层
#! /bin/bash
args=$# # 位置参数的个数
lastarg=${!args}
可以这样解释,假设有5个参数a b c d e
那么$args=5
${!args}的意思是先把args展开,然后以args的值,也就是5,作为参数的名字再次展开作为表达式的值,也就是$5的值,这里就被扩展成了e。
顺便贴上bash手册中的介绍:
The basic form of parameter expansion is ${parameter}. The value of parameter is substituted. The braces are required when parameter is a positional parameter with more than one digit, or when parameter is followed by a character that is not to be interpreted as part of its name.
If the first character of parameter is [color="Red"]an exclamation point, a level of variable indirection is introduced. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. |
|