SSL: 1
Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893
{"id":88,"date":"2017-04-24T21:11:35","date_gmt":"2017-04-24T13:11:35","guid":{"rendered":"https:\/\/blog.weskiller.com\/?p=88"},"modified":"2017-04-25T14:19:31","modified_gmt":"2017-04-25T06:19:31","slug":"bash-%e5%a4%84%e7%90%86%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/blog.gamein.vip\/parameter-expansion.html","title":{"rendered":"Bash Parameter Expansion"},"content":{"rendered":"

\u4eca\u5929\u91cd\u65b0\u6e29\u6545\u4e86\u4e00\u904dshell
\nBash Reference Manual<\/a>
\n\u5728\u9605\u8bfbParameter Expansion\u90e8\u5206\u53d1\u73b0\u4e86\u65b0\u5927\u9646.<\/p>\n

${parameter:-word}<\/span>
\n\u5982\u679cparameter\u4e3a\u7a7a\u6216\u8005\u672a\u5b9a\u4e49,\u503c\u66ff\u6362\u4e3aword;\u5426\u5219\u4e3aparameter\u7684\u503c.<\/p>\n

\r\n#var1=""  var2="world"\r\n#echo ${var1:-hello} ${var2:-guest}\r\nhello world\r\n<\/pre>\n<\/p>\n

${parameter:=word}<\/span>
\n\u5982\u679cparameter\u4e3a\u7a7a\u6216\u8005\u672a\u5b9a\u4e49,word\u8d4b\u503c\u7ed9parameter;\u5426\u5219\u4e3aparameter\u7684\u503c.<\/p>\n

\r\n#var1=""  var2="world"\r\n#echo ${var1:=hello} ${var2:=guest}\r\nhello world\r\n#echo ${var1} ${var2}\r\nhello world\r\n<\/pre>\n<\/p>\n

${parameter:?word}<\/span>
\n\u5982\u679cparameter\u4e3a\u7a7a\u6216\u8005\u672a\u5b9a\u4e49,word\u4f5c\u4e3a\u6807\u51c6\u9519\u8bef\u8f93\u51fa,\u5e76\u4e14\u5982\u679cshell\u975e\u4ea4\u4e92\u5f0f,\u9000\u51fashell;\u5426\u5219\u4e3aparameter\u7684\u503c.<\/p>\n

\r\n#echo ${var:?\u8fd9\u4e2a\u53d8\u91cf\u5b58\u5728\u5417?}\r\n-bash: var: \u8fd9\u4e2a\u53d8\u91cf\u5b58\u5728\u5417?\r\n<\/pre>\n<\/p>\n

${parameter:+word}<\/span>
\n\u5982\u679cparameter\u4e3a\u7a7a\u6216\u8005\u672a\u5b9a\u4e49,\u503c\u4e3a\u7a7a;\u5426\u5219\u4e3aword\u7684\u503c.(\u548c${parameter:-word}<\/span>\u76f8\u53cd)<\/p>\n

\r\n#var1=""  var2="world"\r\n#echo ${var1:+hello} ${var2:+guest}\r\nguest\r\n<\/pre>\n<\/p>\n

${parameter:offset}<\/span>
\n${parameter:offset:length}<\/span>
\n\u8fd9\u662f\u5b50\u5b57\u7b26\u4e32\u7684\u6269\u5c55;
\n\u5982\u679cparameter\u662f\u4e00\u4e2a\u53d8\u91cf,
\n\u503c\u4e3a\u53c2\u6570(\u4ece\u5de6\u5230\u53f3)\u7b2coffset\u7684\u5f00\u59cb\u4fdd\u7559length\u4e2a\u5b57\u7b26;
\n\u5982\u679c\u6ca1\u6709\u6307\u5b9alength,\u503c\u4e3a\u53c2\u6570(\u4ece\u5de6\u5230\u53f3)\u7b2coffset\u7684\u5f00\u59cb\u7684\u5b57\u7b26;
\n\u5982\u679coffset\u5c0f\u4e8e0,\u5219\u4ece\u5c3e\u90e8\u5f00\u59cb(offset\u4e3a\u8d1f\u6570\u65f6,\u4e00\u5b9a\u8981\u52a0\u7a7a\u683c);
\n\u5982\u679clength\u5c0f\u4e8e0,\u5219\u8868\u793a\u4e3a\u4ece\u5c3e\u90e8\u5f00\u59cb\u7684\u504f\u79fb\u91cf.
\n\u5982\u679cparameter\u662f\u4e00\u4e2a\u6570\u7ec4(\u5e26\u4e0b\u6807@<\/span>\u6216\u8005*<\/span>),
\n\u6570\u7ec4\u6210\u5458\u4f5c\u4e3a\u4e00\u4e2a\u5143\u7d20\u504f\u79fb,length\u5fc5\u987b\u5927\u4e8e0.<\/p>\n

\r\n*strings\u4e3a\u53d8\u91cf\r\n#strings=goodluck\r\n#echo ${strings:4:4}\r\nluck\r\n#echo ${strings: -6:2}\r\nod\r\n#echo ${strings: -6:-1}\r\nodluc\r\n*strings\u4e3a\u6570\u7ec4,length\u5fc5\u987b\u5927\u4e8e0\r\n#strings=(this is a arrays)\r\n#echo ${strings[@]:2:1}\r\na\r\n#echo ${strings[@]: -1:1}\r\narrays\r\n#echo ${strings[@]: -1: -1}\r\n-bash:  -1: substring expression < 0\r\n<\/pre>\n<\/p>\n

${!prefix*}<\/span>
\n${!prefix@}<\/span>
\n\u5217\u51fa\u6240\u6709\u5f00\u5934\u4e3aprefix\u7684\u53d8\u91cf\u540d,\u4ee5IFS\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4f5c\u4e3a\u5206\u9694\u7b26.
\n\u5982\u679c\u4f7f\u7528”@<\/span>“,\u5e76\u4e14\u5728\u53cc\u5f15\u53f7\u5185,\u5219\u6bcf\u4e2a\u53d8\u91cf\u540d\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u5355\u8bcd.<\/p>\n

\r\n#declare websit=blog.weskiller.com wad= wait=3\r\n#echo ${!w@}\r\nwad wait websit\r\n#echo ${!wa*}\r\nwad wait\r\n#for i in "${!wa@}";do echo $i;done\r\nwad\r\nwait\r\n#for i in "${!wa*}";do echo $i;done\r\nwad wait\r\n<\/pre>\n<\/p>\n

${!name[@]}<\/span>
\n${!name[*]}<\/span>
\n\u5982\u679cname\u662f\u4e00\u4e2a\u6570\u7ec4\u53d8\u91cf,\u5217\u51fa\u6570\u7ec4\u4e0b\u6807\u6216\u8005\u6570\u7ec4\u952e\u503c(\u5982\u679c\u662f\u952e\u503c\u6570\u7ec4),
\n\u5982\u679c\u4f7f\u7528”@<\/span>“,\u5e76\u4e14\u5728\u53cc\u5f15\u53f7\u5185,\u5219\u6bcf\u4e2a\u952e\u503c\u6216\u8005\u4e0b\u6807\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u5355\u8bcd.;
\n\u5982\u679cname\u662f\u4e00\u4e2a\u53d8\u91cf,\u5219\u503c\u4e3a0;
\n\u5982\u679cname\u4e0d\u662f\u4e00\u4e2a\u6570\u7ec4\u6216\u8005\u53d8\u91cf,\u503c\u4e3a\u7a7a.<\/p>\n

\r\n#declare -A arrays=([a]=1 [b]=2 [c]=3 [d]=4)\r\n#echo ${!arrays[@]}\r\na b c d\r\n#unset arrays\r\n#declare -a arrays=(1 2 3 4 5)\r\n#echo ${!arrays[*]}\r\n0 1 2 3 4\r\n#unset arrays\r\n#decalire arrays=this\r\n#echo ${!arrays[@]}\r\n0\r\n<\/pre>\n<\/p>\n

${#parameter}<\/span>
\n\u503c\u4e3aparameter\u5b57\u7b26\u7684\u957f\u5ea6;
\n\u5982\u679cparameter\u662f\u4e00\u4e2a\u6570\u7ec4(\u5e26\u4e0b\u6807@<\/span>\u6216\u8005*<\/span>),\u5219\u503c\u8868\u793a\u6570\u7ec4\u5305\u542b\u5143\u7d20\u7684\u4e2a\u6570.<\/p>\n

\r\n#strings=abcd\r\n#echo ${#strings}\r\n4\r\n#strings=(a b c d e)\r\n#echo ${#strings[*]}\r\n5\r\n<\/pre>\n<\/p>\n

${parameter#word}<\/span>
\n${parameter##word}<\/span>
\n“*<\/span>“\u8868\u793a\u4e3a\u901a\u914d\u7b26,\u8868\u793a\u4efb\u610f\u5b57\u7b26\u4efb\u610f\u957f\u5ea6,
\n“?<\/span>“\u8868\u793a\u4e3a\u901a\u914d\u7b26,\u8868\u793a\u4e00\u4e2a\u4efb\u610f\u5b57\u7b26,
\n“[..]<\/span>“\u8868\u793a\u5339\u914d[]\u5185\u5305\u542b\u7684\u5355\u4e2a\u5b57\u7b26.
\n\u8be6\u7ec6\u8d44\u6599\u8bf7\u79fb\u6b65\u67e5\u770b\u624b\u518cFilename-Expansion<\/a>
\nword\u4f5c\u4e3a\u6587\u4ef6\u540d\u6269\u5c55\u6a21\u5f0f,\u4eceparameter\u7684\u503c\u5934\u90e8\u5f00\u59cb\u5339\u914d,\u5220\u9664\u5339\u914d\u6700\u77ed\u7684\u90e8\u5206(#),\u6216\u8005\u5220\u9664\u6700\u957f\u5339\u914d\u7684\u90e8\u5206(##).
\n\u5982\u679cparameter\u662f\u4e00\u4e2a\u6570\u7ec4(\u5e26\u4e0b\u6807@<\/span>\u6216\u8005*<\/span>),
\n\u5219\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5339\u914d\uff0c\u7ed3\u679c\u4e3a\u5220\u9664\u540e\u7684\u503c.<\/p>\n

\r\n#strings="hello world"\r\n#echo ${strings#h*l}\r\nlo world\r\necho ${strings##h*l}\r\nd\r\n<\/pre>\n

<\/P>
\n${parameter%word}<\/span>
\n${parameter%%word}<\/span>
\n\u548c${parameter#word},${parameter##word}<\/span>\u76f8\u540c,\u4f46\u662f\u4ece\u5c3e\u90e8\u5f00\u59cb\u5339\u914d.<\/p>\n

\r\n#strings="hello world"\r\n# echo ${strings%l*d}\r\nhello wor\r\n# echo ${strings%%l*d}\r\nhe\r\n<\/pre>\n<\/p>\n

${parameter\/pattern\/string}<\/span>
\npattern\u5339\u914dparameter\u7684\u503c,\u66ff\u6362\u4e3astring,\/\u66ff\u6362\u7b2c\u4e00\u6b21\u5339\u914d,
\n\u5982\u679cpattern\u5f00\u5934\u4e3a\/,\u5219\u66ff\u6362\u6240\u6709\u5339\u914d;
\npattern\u4f5c\u4e3a\u6587\u4ef6\u540d\u6269\u5c55\u6a21\u5f0f.<\/p>\n

\r\n#strings=abcdea\r\n#echo ${strings\/a\/0}\r\n0bcdea\r\n#echo ${strings\/\/a\/0}\r\n0bcde0\r\n<\/pre>\n

${parameter^pattern}<\/span>
\n${parameter^^pattern}<\/span>
\n\u6539\u53d8parameter\u7684\u503c,\u8f6c\u6362\u5c0f\u5199\u5b57\u6bcd\u4e3a\u5927\u5199\uff0c\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e3a\u5927\u5199(^),\u6216\u8005\u5168\u90e8\u8f6c\u6362\u4e3a\u5927\u5199(^^).<\/p>\n

\r\n#shell=bash\r\n#echo ${shell^}\r\nBash\r\n#echo ${shell^^}\r\nBASH\r\n<\/pre>\n<\/p>\n

${parameter,pattern}<\/span>
\n${parameter,,pattern}<\/span>
\n\u6539\u53d8parameter\u7684\u503c,\u8f6c\u6362\u5927\u5199\u5b57\u6bcd\u4e3a\u5c0f\u5199\uff0c\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e3a\u5c0f\u5199(,),\u6216\u8005\u5168\u90e8\u8f6c\u6362\u4e3a\u5c0f\u5199(,,).<\/p>\n

\r\n#shell=BASH\r\n#echo ${shell,}\r\nbASH\r\n#echo ${shell,,}\r\nbash\r\n<\/pre>\n

\u9646\u7eed\u66f4\u65b0<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4eca\u5929\u91cd\u65b0\u6e29\u6545\u4e86\u4e00\u904dshell Bash Reference Manual \u5728\u9605\u8bfbParameter Expan […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,2],"tags":[8],"class_list":["post-88","post","type-post","status-publish","format-standard","hentry","category-linux","category-shell","tag-8"],"_links":{"self":[{"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/posts\/88","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":30,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":637,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/posts\/88\/revisions\/637"}],"wp:attachment":[{"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}