我今天遇到了一个 bash 脚本,它有以下引导线:
$ cat -n deploy.sh
1 #!/bin/bash
2
3 # Usage: ./deploy.sh [host]
4
5 host="${1:-ubuntu@example.com}"
6
7 # The host key might change when we instantiate a new VM, so
8 # we remove (-R) the old host key from known_hosts
9 ssh-keygen -R "${host#*@}" 2> /dev/null
[...]
5号线很简单。9号线接住了我。我“相信”这是一种 bash 参数扩展,但是阅读手册页,我不再那么确定了。
引用 bash 手册页:
${parameter#word}
${parameter##word}
Remove matching prefix pattern. The word is expanded to produce
a pattern just as in pathname expansion. If the pattern matches
the beginning of the value of parameter, then the result of the
expansion is the expanded value of parameter with the shortest
matching pattern (the ``#'' case) or the longest matching pat‐
tern (the ``##'' case) deleted. If parameter is @ or *, the
pattern removal operation is applied to each positional parame‐
ter in turn, and the expansion is the resultant list. If param‐
eter is an array variable subscripted with @ or *, the pattern
removal operation is applied to each member of the array in
turn, and the expansion is the resultant list.
可以说我只是像这样运行脚本
./deploy.sh
没有任何输入参数,然后到第 5 行,host 将设置为 ubuntu@example.com。然后来到第 9 行,${host#*@} 开始发挥作用。# 使用扩展的 *@ 触发替换。但它扩展到什么?这不是手册页中使用的词吗?
任何提示/提示表示赞赏。
扎克