我今天遇到了一個bash腳本,它具有以下主要線路:如何bash shell參數展開這個表達式?
$ cat -n deploy.sh
1 #!/bin/bash
2
3 # Usage: ./deploy.sh [host]
4
5 host="${1:[email protected]}"
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號線,主機將被設置爲Ubuntu的@ example.com。然後來到9號線,$ {host#* @}進場。 #使用擴展的* @觸發替換。但是它擴展到了什麼?這不是用於手冊頁的單詞嗎?
任何提示/提示表示讚賞。
扎克
嘗試添加'echo「$ {host#* @}」' – perreal