Linux Shell參數擴充(Parameter Expansion),parameterexpansion

來源:互聯網
上載者:User

Linux Shell參數擴充(Parameter Expansion),parameterexpansion

本文主要參考:http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02

其他資料:http://www.ibm.com/developerworks/cn/linux/l-bash-parameters.html


參數擴充的表示形式為:${expression}。expression包括各種字元直到匹配上'}'。當出現以下情況時候'}'不會被檢查來匹配:

1)在逸出字元\之後,如\{;

2)在引號裡面,如‘}’;

3) 在算術運算式,命令替換或者變數擴充裡面的,如${value}


最簡單的參數擴充形式如:${parameter}

使用如下模式可以修改參數擴充:

${ parameter:- [ word ]}
Use Default Values. If  parameter is unset or null, the expansion of  word (or an empty string if  word is omitted) shall be substituted; otherwise, the value of  parameter shall be substituted.
當${parameter}值為空白或者沒有設定的時候,用[word]值來替換,否則它就是該運算式的值。
[hdfs@cdh51kdc ~]$ bb=3[hdfs@cdh51kdc ~]$ echo ${aa}[hdfs@cdh51kdc ~]$ echo ${bb}3[hdfs@cdh51kdc ~]$ echo ${aa-${bb}}3[hdfs@cdh51kdc ~]$ aa=2[hdfs@cdh51kdc ~]$ echo ${aa-${bb}}2
${ parameter:= [ word ]}
Assign Default Values. If  parameter is unset or null, the expansion of  word (or an empty string if  word is omitted) shall be assigned to  parameter. In all cases, the final value of  parameter shall be substituted. Only variables, not positional parameters or special parameters, can be assigned in this way.
當${parameter}值為空白或者沒有設定的時候,用[word]值來給${parameter}賦值並替換最後的運算式,否則它就是該運算式的值。
[hdfs@cdh51kdc ~]$ echo ${aa-${bb}}2[hdfs@cdh51kdc ~]$ echo ${aa:=${bb}}2[hdfs@cdh51kdc ~]$ echo ${cc}[hdfs@cdh51kdc ~]$ echo ${cc:=${bb}}3[hdfs@cdh51kdc ~]$ echo ${cc}3
${ parameter:? [ word ]}
Indicate Error if Null or Unset. If  parameter is unset or null, the expansion of  word (or a message indicating it is unset if  word is omitted) shall be written to standard error and the shell exits with a non-zero exit status. Otherwise, the value of  parameter shall be substituted. An interactive shell need not exit.
當${parameter}值為空白或者沒有設定的時候,用[word]值作為標準錯誤輸出提示並退出shell且返回非0狀態。否則它就是該運算式的值。
[hdfs@cdh51kdc ~]$ echo ${cc:?"Value not set"}3[hdfs@cdh51kdc ~]$ echo ${dd:?"Value not set"}-bash: dd: Value not set

${ parameter:+ [ word ]}
Use Alternative Value. If  parameter is unset or null, null shall be substituted; otherwise, the expansion of  word (or an empty string if  word is omitted) shall be substituted.
當${parameter}值為空白或者沒有設定的時候,運算式返回null。否則用[word]替換運算式的值。
[hdfs@cdh51kdc ~]$ echo ${cc:+"Value not set"}Value not set[hdfs@cdh51kdc ~]$ echo ${dd:+"Value not set"}

${#parameter}

String Length. The length in characters of the value of  parameter shall be substituted. If  parameter is '*' or '@', the result of the expansion is unspecified. If  parameter is unset and  set  -u is in effect, the expansion shall fail.
運算式返回${parameter}值中字元的個數。
[hdfs@cdh51kdc ~]$ echo ${#cc}1[hdfs@cdh51kdc ~]$ echo ${#dd}0

The following four varieties of parameter expansion provide for substring processing. In each case, pattern matching notation (see Pattern Matching Notation), rather than regular expression notation, shall be used to evaluate the patterns. If parameter is '#', '*', or '@', the result of the expansion is unspecified. If parameter is unset and set -u is in effect, the expansion shall fail. Enclosing the full parameter expansion string in double-quotes shall not cause the following four varieties of pattern characters to be quoted, whereas quoting characters within the braces shall have this effect. In each variety, if word is omitted, the empty pattern shall be used.

${parameter%[word]}

Remove Smallest Suffix Pattern. The  word shall be expanded to produce a pattern. The parameter expansion shall then result in  parameter, with the smallest portion of the suffix matched by the  pattern deleted. If present, word shall not begin with an unquoted '%'.
用[word]產生的模式來匹配${parameter}的尾碼並去除掉最小匹配部分
[hdfs@cdh51kdc ~]$ echo ${cc}Value not set[hdfs@cdh51kdc ~]$ echo ${cc%"et"}Value not s
${ parameter%% [ word ]}
Remove Largest Suffix Pattern. The  word shall be expanded to produce a pattern. The parameter expansion shall then result in  parameter, with the largest portion of the suffix matched by the  pattern deleted.
用[word]產生的模式來匹配${parameter}的尾碼並去除掉最大匹配部分
[hdfs@cdh51kdc ~]$ echo ${cc%%t*}Value no[hdfs@cdh51kdc ~]$ echo ${cc%t*}Value not se
${ parameter# [ word ]}
Remove Smallest Prefix Pattern. The  word shall be expanded to produce a pattern. The parameter expansion shall then result in  parameter, with the smallest portion of the prefix matched by the  pattern deleted. If present,  word shall not begin with an unquoted '#'.
${ parameter## [ word ]}
Remove Largest Prefix Pattern. The  word shall be expanded to produce a pattern. The parameter expansion shall then result in  parameter, with the largest portion of the prefix matched by the  pattern deleted.
最後兩個分別是去除掉首碼的[word]最小匹配和最大匹配
[hdfs@cdh51kdc ~]$ echo ${cc#*t}set[hdfs@cdh51kdc ~]$ echo ${cc##*t}[hdfs@cdh51kdc ~]$ echo ${cc#V}alue not set



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.