{"id":512,"date":"2014-07-09T01:45:23","date_gmt":"2014-07-09T01:45:23","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=512"},"modified":"2014-07-09T01:45:23","modified_gmt":"2014-07-09T01:45:23","slug":"linux-shell%e8%84%9a%e6%9c%ac%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%88%aa%e5%8f%96%e7%ad%89%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/512.html","title":{"rendered":"Linux shell\u811a\u672c\u7684\u5b57\u7b26\u4e32\u622a\u53d6\u7b49\u64cd\u4f5c"},"content":{"rendered":"<h5 style=\"color: #000000;\"><strong><span style=\"font-size: medium;\">Linux \u7684\u5b57\u7b26\u4e32\u622a\u53d6\u7684\u516b\u79cd\u65b9\u6cd5\uff1a<\/span><\/strong><\/h5>\n<div>\n<p style=\"color: #000000;\">\u5047\u8bbe\u6709\u53d8\u91cf\uff1a<strong><span style=\"color: #ff0000;\">var=http:\/\/www.hao.com\/123.htm<\/span><\/strong><\/p>\n<h6 style=\"color: #000000;\"><strong><span style=\"font-size: medium;\"><span style=\"color: #0000ff;\">\u4e00\u3001# \u53f7\u622a\u53d6\uff0c<\/span><span style=\"color: #0000ff;\">\u5220\u9664\u5de6\u8fb9\u5b57\u7b26\uff0c\u4fdd\u7559\u53f3\u8fb9\u5b57\u7b26<\/span><span style=\"color: #0000ff;\">\u3002\uff08<\/span><span style=\"color: #ff0000;\">\u6700\u77ed\u5339\u914d<\/span><span style=\"color: #0000ff;\">\uff09<\/span><\/span><\/strong><\/h6>\n<p style=\"color: #000000;\"><strong style=\"color: #ff0000;\"><span style=\"font-size: medium;\">echo ${var#*\/\/}<\/span><\/strong><\/p>\n<p style=\"color: #000000;\"><span style=\"font-size: medium;\">\u5176\u4e2d var \u662f\u53d8\u91cf\u540d\uff0c# \u53f7\u662f\u8fd0\u7b97\u7b26\uff0c<span style=\"color: #ff0000;\"><strong>*\/\/\u00a0<\/strong><\/span>\u8868\u793a\u4ece\u5de6\u8fb9\u5f00\u59cb\u5220\u9664\u7b2c\u4e00\u4e2a \/\/ \u53f7\u53ca\u5de6\u8fb9\u7684\u6240\u6709\u5b57\u7b26<\/span><\/p>\n<p style=\"color: #000000;\"><span style=\"font-size: medium;\">\u5373\u5220\u9664\u00a0<strong><span style=\"color: #ff0000;\">http:\/\/<\/span><\/strong><\/span><\/p>\n<p style=\"color: #000000;\"><span style=\"font-size: medium;\">\u7ed3\u679c\u662f \uff1a<strong><span style=\"color: #ff0000;\">www.hao.com\/123.htm<\/span><\/strong><\/span><\/p>\n<h6 style=\"color: #000000;\"><span style=\"color: #0000ff;\"><strong><span style=\"font-size: medium;\">\u4e8c\u3001## \u53f7\u622a\u53d6\uff0c\u5220\u9664\u5de6\u8fb9\u5b57\u7b26\uff0c\u4fdd\u7559\u53f3\u8fb9\u5b57\u7b26\u3002<\/span><\/strong><\/span>\uff08<b><span style=\"color: #ff0000;\">\u6700\u957f\u5339\u914d<\/span><\/b>\uff09<\/h6>\n<p style=\"color: #000000;\"><strong><span style=\"color: #ff0000;\"><span style=\"font-size: medium;\">echo ${var##*\/}<\/span><\/span><\/strong><\/p>\n<p style=\"color: #000000;\"><span style=\"font-size: medium;\"><strong style=\"color: #ff0000;\">##*\/\u00a0<\/strong>\u8868\u793a\u4ece\u5de6\u8fb9\u5f00\u59cb\u5220\u9664\u6700\u540e\uff08\u6700\u53f3\u8fb9\uff09\u4e00\u4e2a \/ \u53f7\u53ca\u5de6\u8fb9\u7684\u6240\u6709\u5b57\u7b26<\/span><\/p>\n<p style=\"color: #000000;\"><span style=\"color: #ff0000;\"><span style=\"font-size: medium;\"><span style=\"color: #000000;\">\u5373\u5220\u9664\u00a0<\/span><strong>http:\/\/www.hao.com\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/strong><\/span><\/span><\/p>\n<p style=\"color: #000000;\"><span style=\"color: #ff0000;\"><strong><span style=\"font-size: medium;\">\u7ed3\u679c\u662f 123.htm<\/span><\/strong><\/span><\/p>\n<h6 style=\"color: #000000;\"><span style=\"color: #0000ff;\"><strong><span style=\"font-size: medium;\">\u4e09\u3001%\u53f7\u622a\u53d6\uff0c\u5220\u9664\u53f3\u8fb9\u5b57\u7b26\uff0c\u4fdd\u7559\u5de6\u8fb9\u5b57\u7b26\u3002<\/span><\/strong><\/span>\uff08<b><span style=\"color: #ff0000;\">\u6700\u77ed\u5339\u914d<\/span><\/b>\uff09<\/h6>\n<p style=\"color: #000000;\"><span style=\"color: #ff0000;\"><strong><span style=\"font-size: medium;\">echo ${var%\/*}<\/span><\/strong><\/span><\/p>\n<p style=\"color: #000000;\"><strong><span style=\"font-size: medium;\"><span style=\"color: #ff0000;\">%\/*\u00a0\u8868\u793a\u4ece\u53f3\u8fb9\u5f00\u59cb\uff0c\u5220\u9664<\/span><span style=\"color: #1f497d;\">\u7b2c\u4e00\u4e2a<\/span><span style=\"color: #ff0000;\">\u00a0\/ \u53f7\u53ca\u53f3\u8fb9\u7684\u5b57\u7b26<\/span><\/span><\/strong><\/p>\n<p style=\"color: #000000;\"><strong><span style=\"font-size: medium;\">\u7ed3\u679c\u662f\uff1a<span style=\"color: #ff0000;\">http:\/\/www.hao.com<\/span><\/span><\/strong><\/p>\n<h6><strong><span style=\"font-size: medium;\"><span style=\"color: #0000ff;\">\u56db\u3001<\/span><span style=\"color: #0000ff;\">%% \u53f7\u622a\u53d6\uff0c\u5220\u9664\u53f3\u8fb9\u5b57\u7b26\uff0c\u4fdd\u7559\u5de6\u8fb9\u5b57\u7b26\u3002<\/span><\/span><\/strong><span style=\"color: #000000;\">\uff08<\/span><b style=\"color: #000000;\"><span style=\"color: #ff0000;\">\u6700\u957f\u5339\u914d<\/span><\/b><span style=\"color: #000000;\">\uff09<\/span><\/h6>\n<p style=\"color: #000000;\"><strong><span style=\"color: #ff0000;\"><strong><span style=\"font-size: medium;\">echo ${var%%\/*}<\/span><\/strong><\/span><\/strong><\/p>\n<p style=\"color: #000000;\"><strong><span style=\"color: #ff0000;\">%%\/*\u00a0<\/span>\u8868\u793a\u4ece\u53f3\u8fb9\u5f00\u59cb\uff0c\u5220\u9664<span style=\"color: #ff0000;\">\u6700\u540e\uff08\u6700\u5de6\u8fb9\uff09<\/span>\u4e00\u4e2a \/ \u53f7\u53ca\u53f3\u8fb9\u7684\u5b57\u7b26<\/strong><\/p>\n<p style=\"color: #000000;\"><strong>\u7ed3\u679c\u662f\uff1a<span style=\"color: #ff0000;\">http:<\/span><\/strong><\/p>\n<h6 style=\"color: #000000;\"><strong><span style=\"color: #ff0000;\">\u4e94<span style=\"color: #0000ff;\">\u3001<\/span>\u4ece\u5de6\u8fb9\u7b2c\u51e0\u4e2a\u5b57\u7b26\u5f00\u59cb\uff0c\u53ca\u5b57\u7b26\u7684\u4e2a\u6570<\/span><\/strong><\/h6>\n<p style=\"color: #000000;\"><strong><span style=\"color: #ff0000;\">echo ${var:0:5}<\/span><\/strong><\/p>\n<p style=\"color: #000000;\">\u5176\u4e2d\u7684 0 \u8868\u793a\u5de6\u8fb9\u7b2c\u4e00\u4e2a\u5b57\u7b26<span style=\"color: #e60013;\">\u5f00\u59cb<\/span>\uff0c5 \u8868\u793a\u5b57\u7b26\u7684\u603b\u4e2a\u6570\uff08<span style=\"color: #e60013;\">\u957f\u5ea6<\/span>\uff09\u3002<\/p>\n<p style=\"color: #000000;\">\u7ed3\u679c\u662f\uff1a<span style=\"color: #ff0000;\">http:<\/span><\/p>\n<h6 style=\"color: #000000;\"><span style=\"color: #ff0000;\">\u516d<span style=\"font-weight: bold;\"><span style=\"color: #0000ff;\">\u3001<\/span><\/span>\u4ece\u5de6\u8fb9\u7b2c\u51e0\u4e2a\u5b57<\/span><span style=\"color: #ff0000;\">\u7b26\u5f00\u59cb\uff0c\u4e00\u76f4\u5230\u7ed3\u675f<\/span><\/h6>\n<p style=\"color: #000000;\"><span style=\"color: #ff0000;\">echo ${var:7}<\/span><\/p>\n<p style=\"color: #000000;\">\u5176\u4e2d\u7684 7 \u8868\u793a\u5de6\u8fb9\u7b2c8\u4e2a\u5b57\u7b26\u5f00\u59cb\uff0c\u4e00\u76f4\u5230\u7ed3\u675f\u3002<\/p>\n<p style=\"color: #000000;\">\u7ed3\u679c\u662f\u00a0\uff1a<strong><span style=\"color: #ff0000;\">www.hao.com\/123.htm<\/span><\/strong><\/p>\n<h6 style=\"color: #000000;\"><strong><span style=\"color: #ff0000;\">\u4e03<span style=\"color: #0000ff;\">\u3001<\/span><span style=\"color: #000000;\"><strong><span style=\"color: #ff0000;\">\u4ece\u53f3\u8fb9\u7b2c\u51e0\u4e2a\u5b57\u7b26\u5f00\u59cb\uff0c\u53ca\u5b57\u7b26\u7684\u4e2a\u6570<\/span><\/strong><\/span><\/span><\/strong><\/h6>\n<p style=\"color: #000000;\"><strong><span style=\"color: #ff0000;\">echo ${var:0-7:3}<\/span><\/strong><\/p>\n<p style=\"color: #000000;\"><span style=\"color: #0000ff;\">\u5176\u4e2d\u7684\u00a0<span style=\"color: #ff0000;\">0-7<\/span>\u00a0\u8868\u793a\u53f3\u8fb9\u7b97\u8d77\u7b2c\u4e03\u4e2a\u5b57\u7b26\u5f00\u59cb\uff0c3 \u8868\u793a\u5b57\u7b26\u7684\u4e2a\u6570\u3002<\/span><\/p>\n<p style=\"color: #000000;\">\u7ed3\u679c\u662f\uff1a123<\/p>\n<h6 style=\"color: #000000;\"><strong><span style=\"color: #0000ff;\">\u516b\u3001\u4ece\u53f3\u8fb9\u7b2c\u51e0\u4e2a\u5b57\u7b26\u5f00\u59cb\uff0c\u4e00\u76f4\u5230\u7ed3\u675f<\/span><\/strong><\/h6>\n<p style=\"color: #000000;\">echo ${var:0-7}<\/p>\n<p style=\"color: #000000;\">\u8868\u793a\u4ece\u53f3\u8fb9\u7b2c\u4e03\u4e2a\u5b57\u7b26\u5f00\u59cb\uff0c\u4e00\u76f4\u5230\u7ed3\u675f\u3002<\/p>\n<p style=\"color: #000000;\">\u7ed3\u679c\u662f\uff1a123.htm\u3002<\/p>\n<p style=\"color: #000000;\">\u6ce8\uff1a\uff08\u5de6\u8fb9\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u662f\u7528 0 \u8868\u793a\uff0c\u53f3\u8fb9\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7528 0-1 \u8868\u793a\uff09<\/p>\n<h6 style=\"color: #000000;\">\u539f\u6587\u5730\u5740\uff1a<\/h6>\n<p style=\"color: #000000;\"><a href=\"http:\/\/blog.csdn.net\/gumanren\/article\/details\/5601544\" target=\"_blank\">http:\/\/blog.csdn.net\/gumanren\/article\/details\/5601544<\/a><\/p>\n<hr style=\"color: #000000;\" \/>\n<h4><a style=\"font-weight: 800; color: navy;\" href=\"http:\/\/www.cnblogs.com\/chengmo\/archive\/2010\/10\/02\/1841355.html\" target=\"_blank\">linux shell \u5b57\u7b26\u4e32\u64cd\u4f5c\uff08\u957f\u5ea6\uff0c\u67e5\u627e\uff0c\u66ff\u6362\uff09\u8be6\u89e3<\/a><\/h4>\n<p>\u5728\u505ashell\u6279\u5904\u7406\u7a0b\u5e8f\u65f6\u5019\uff0c\u7ecf\u5e38\u4f1a\u6d89\u53ca\u5230\u5b57\u7b26\u4e32\u76f8\u5173\u64cd\u4f5c\u3002\u6709\u5f88\u591a\u547d\u4ee4\u8bed\u53e5\uff0c\u5982\uff1aawk,sed\u90fd\u53ef\u4ee5\u505a\u5b57\u7b26\u4e32\u5404\u79cd\u64cd\u4f5c\u3002\u00a0\u5176\u5b9eshell\u5185\u7f6e\u4e00\u7cfb\u5217\u64cd\u4f5c\u7b26\u53f7\uff0c\u53ef\u4ee5\u8fbe\u5230\u7c7b\u4f3c\u6548\u679c\uff0c\u5927\u5bb6\u77e5\u9053\uff0c\u4f7f\u7528\u5185\u90e8\u64cd\u4f5c\u7b26\u4f1a\u7701\u7565\u542f\u52a8\u5916\u90e8\u7a0b\u5e8f\u7b49\u65f6\u95f4\uff0c\u56e0\u6b64\u901f\u5ea6\u4f1a\u975e\u5e38\u7684\u5feb\u3002<\/p>\n<h5>\u4e00\u3001\u5224\u65ad\u8bfb\u53d6\u5b57\u7b26\u4e32\u503c<\/h5>\n<table>\n<tbody>\n<tr>\n<td width=\"14\">\u8868\u8fbe\u5f0f<\/td>\n<td width=\"14\">\u542b\u4e49<\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${var}<\/td>\n<td width=\"14\">\u53d8\u91cfvar\u7684\u503c,\u00a0\u4e0e$var\u76f8\u540c<\/td>\n<\/tr>\n<tr>\n<td width=\"14\"><\/td>\n<td width=\"14\"><\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${var-DEFAULT}<\/td>\n<td width=\"14\">\u5982\u679cvar\u6ca1\u6709\u88ab\u58f0\u660e,\u00a0\u90a3\u4e48\u5c31\u4ee5$DEFAULT\u4f5c\u4e3a\u5176\u503c\u00a0*<\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${var:-DEFAULT}<\/td>\n<td width=\"14\">\u5982\u679cvar\u6ca1\u6709\u88ab\u58f0\u660e,\u00a0\u6216\u8005\u5176\u503c\u4e3a\u7a7a,\u00a0\u90a3\u4e48\u5c31\u4ee5$DEFAULT\u4f5c\u4e3a\u5176\u503c\u00a0*<\/td>\n<\/tr>\n<tr>\n<td width=\"14\"><\/td>\n<td width=\"14\"><\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${var=DEFAULT}<\/td>\n<td width=\"14\">\u5982\u679cvar\u6ca1\u6709\u88ab\u58f0\u660e,\u00a0\u90a3\u4e48\u5c31\u4ee5$DEFAULT\u4f5c\u4e3a\u5176\u503c\u00a0*<\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${var:=DEFAULT}<\/td>\n<td width=\"14\">\u5982\u679cvar\u6ca1\u6709\u88ab\u58f0\u660e,\u00a0\u6216\u8005\u5176\u503c\u4e3a\u7a7a,\u00a0\u90a3\u4e48\u5c31\u4ee5$DEFAULT\u4f5c\u4e3a\u5176\u503c\u00a0*<\/td>\n<\/tr>\n<tr>\n<td width=\"14\"><\/td>\n<td width=\"14\"><\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${var+OTHER}<\/td>\n<td width=\"14\">\u5982\u679cvar\u58f0\u660e\u4e86,\u00a0\u90a3\u4e48\u5176\u503c\u5c31\u662f$OTHER,\u00a0\u5426\u5219\u5c31\u4e3anull\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${var:+OTHER}<\/td>\n<td width=\"14\">\u5982\u679cvar\u88ab\u8bbe\u7f6e\u4e86,\u00a0\u90a3\u4e48\u5176\u503c\u5c31\u662f$OTHER,\u00a0\u5426\u5219\u5c31\u4e3anull\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td width=\"14\"><\/td>\n<td width=\"14\"><\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${var?ERR_MSG}<\/td>\n<td width=\"14\">\u5982\u679cvar\u6ca1\u88ab\u58f0\u660e,\u00a0\u90a3\u4e48\u5c31\u6253\u5370$ERR_MSG\u00a0*<\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${var:?ERR_MSG}<\/td>\n<td width=\"14\">\u5982\u679cvar\u6ca1\u88ab\u8bbe\u7f6e,\u00a0\u90a3\u4e48\u5c31\u6253\u5370$ERR_MSG\u00a0*<\/td>\n<\/tr>\n<tr>\n<td width=\"14\"><\/td>\n<td width=\"14\"><\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${!varprefix*}<\/td>\n<td width=\"14\">\u5339\u914d\u4e4b\u524d\u6240\u6709\u4ee5varprefix\u5f00\u5934\u8fdb\u884c\u58f0\u660e\u7684\u53d8\u91cf<\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${!varprefix@}<\/td>\n<td width=\"14\">\u5339\u914d\u4e4b\u524d\u6240\u6709\u4ee5varprefix\u5f00\u5934\u8fdb\u884c\u58f0\u660e\u7684\u53d8\u91cf<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u52a0\u5165\u4e86\u201c*\u201d\u7684\u8868\u793a\u610f\u601d\u662f\uff1a\u5f53\u7136,\u00a0\u5982\u679c\u53d8\u91cfvar\u5df2\u7ecf\u88ab\u8bbe\u7f6e\u7684\u8bdd,\u00a0\u90a3\u4e48\u5176\u503c\u5c31\u662f$var.<\/p>\n<pre class=\"lang:default decode:true\">[chengmo@localhost\u00a0~]$\u00a0echo\u00a0${abc-'ok'}\nok\n[chengmo@localhost\u00a0~]$\u00a0echo\u00a0$abc\n\n[chengmo@localhost\u00a0~]$\u00a0echo\u00a0${abc='ok'}\nok\n[chengmo@localhost\u00a0~]$\u00a0echo\u00a0$abc\nok<\/pre>\n<p>\u5982\u679cabc\u00a0\u6ca1\u6709\u58f0\u660e\u201c=&#8221;\u00a0\u8fd8\u4f1a\u7ed9abc\u8d4b\u503c\u3002<\/p>\n<pre class=\"lang:default decode:true\">[chengmo@localhost\u00a0~]$\u00a0var1=11;var2=12;var3=\n[chengmo@localhost\u00a0~]$\u00a0echo\u00a0${!v@}\nvar1\u00a0var2\u00a0var3\n[chengmo@localhost\u00a0~]$\u00a0echo\u00a0${!v*}\nvar1\u00a0var2\u00a0var3<\/pre>\n<p>${!varprefix*}\u4e0e${!varprefix@}\u76f8\u4f3c\uff0c\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u540d\u524d\u7f00\u5b57\u7b26\uff0c\u641c\u7d22\u5df2\u7ecf\u5b9a\u4e49\u7684\u53d8\u91cf,\u65e0\u8bba\u662f\u5426\u4e3a\u7a7a\u503c\u3002<\/p>\n<h5>\u4e8c\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\uff08\u957f\u5ea6\uff0c\u8bfb\u53d6\uff0c\u66ff\u6362\uff09<\/h5>\n<table>\n<tbody>\n<tr>\n<td width=\"14\">\u8868\u8fbe\u5f0f<\/td>\n<td width=\"14\">\u542b\u4e49<\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${#string}<\/td>\n<td width=\"14\">$string\u7684\u957f\u5ea6<\/td>\n<\/tr>\n<tr>\n<td width=\"14\"><\/td>\n<td width=\"14\"><\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${string:position}<\/td>\n<td width=\"14\">\u5728$string\u4e2d,\u00a0\u4ece\u4f4d\u7f6e$position\u5f00\u59cb\u63d0\u53d6\u5b50\u4e32<\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${string:position:length}<\/td>\n<td width=\"14\">\u5728$string\u4e2d,\u00a0\u4ece\u4f4d\u7f6e$position\u5f00\u59cb\u63d0\u53d6\u957f\u5ea6\u4e3a$length\u7684\u5b50\u4e32<\/td>\n<\/tr>\n<tr>\n<td width=\"14\"><\/td>\n<td width=\"14\"><\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${string#substring}<\/td>\n<td width=\"14\">\u4ece\u53d8\u91cf$string\u7684\u5f00\u5934,\u00a0\u5220\u9664\u6700\u77ed\u5339\u914d$substring\u7684\u5b50\u4e32<\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${string##substring}<\/td>\n<td width=\"14\">\u4ece\u53d8\u91cf$string\u7684\u5f00\u5934,\u00a0\u5220\u9664\u6700\u957f\u5339\u914d$substring\u7684\u5b50\u4e32<\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${string%substring}<\/td>\n<td width=\"14\">\u4ece\u53d8\u91cf$string\u7684\u7ed3\u5c3e,\u00a0\u5220\u9664\u6700\u77ed\u5339\u914d$substring\u7684\u5b50\u4e32<\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${string%%substring}<\/td>\n<td width=\"14\">\u4ece\u53d8\u91cf$string\u7684\u7ed3\u5c3e,\u00a0\u5220\u9664\u6700\u957f\u5339\u914d$substring\u7684\u5b50\u4e32<\/td>\n<\/tr>\n<tr>\n<td width=\"14\"><\/td>\n<td width=\"14\"><\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${string\/substring\/replacement}<\/td>\n<td width=\"14\">\u4f7f\u7528$replacement,\u00a0\u6765\u4ee3\u66ff\u7b2c\u4e00\u4e2a\u5339\u914d\u7684$substring<\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${string\/\/substring\/replacement}<\/td>\n<td width=\"14\">\u4f7f\u7528$replacement,\u00a0\u4ee3\u66ff\u6240\u6709\u5339\u914d\u7684$substring<\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${string\/#substring\/replacement}<\/td>\n<td width=\"14\">\u5982\u679c$string\u7684\u524d\u7f00\u5339\u914d$substring,\u00a0\u90a3\u4e48\u5c31\u7528$replacement\u6765\u4ee3\u66ff\u5339\u914d\u5230\u7684$substring<\/td>\n<\/tr>\n<tr>\n<td width=\"14\">${string\/%substring\/replacement}<\/td>\n<td width=\"14\">\u5982\u679c$string\u7684\u540e\u7f00\u5339\u914d$substring,\u00a0\u90a3\u4e48\u5c31\u7528$replacement\u6765\u4ee3\u66ff\u5339\u914d\u5230\u7684$substring<\/td>\n<\/tr>\n<tr>\n<td width=\"14\"><\/td>\n<td width=\"14\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8bf4\u660e\uff1a&#8221;*\u00a0$substring\u201d\u53ef\u4ee5\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f.<\/p>\n<h6>1.\u957f\u5ea6<\/h6>\n<pre class=\"lang:default decode:true\">[web97@salewell97\u00a0~]$\u00a0test='I\u00a0love\u00a0china'\n[web97@salewell97\u00a0~]$\u00a0echo\u00a0${#test}\n12<\/pre>\n<p>${#\u53d8\u91cf\u540d}\u5f97\u5230\u5b57\u7b26\u4e32\u957f\u5ea6<\/p>\n<h6>2.\u622a\u53d6\u5b57\u4e32<\/h6>\n<pre class=\"lang:default decode:true\">[chengmo@localhost\u00a0~]$\u00a0test='I\u00a0love\u00a0china'\n[chengmo@localhost\u00a0~]$\u00a0echo\u00a0${test:5}\ne\u00a0china\n[chengmo@localhost\u00a0~]$\u00a0echo\u00a0${test:5:10}\ne\u00a0china<\/pre>\n<p>${\u53d8\u91cf\u540d:\u8d77\u59cb:\u957f\u5ea6}\u5f97\u5230\u5b50\u5b57\u7b26\u4e32<\/p>\n<h6>3.\u5b57\u7b26\u4e32\u5220\u9664<\/h6>\n<pre class=\"lang:default decode:true\">[chengmo@localhost\u00a0~]$\u00a0test='c:\/windows\/boot.ini'\n[chengmo@localhost\u00a0~]$\u00a0echo\u00a0${test#\/}\nc:\/windows\/boot.ini\n[chengmo@localhost\u00a0~]$\u00a0echo\u00a0${test#*\/}\nwindows\/boot.ini\n[chengmo@localhost\u00a0~]$\u00a0echo\u00a0${test##*\/}\nboot.ini\n\n[chengmo@localhost\u00a0~]$\u00a0echo\u00a0${test%\/*}\nc:\/windows\n[chengmo@localhost\u00a0~]$\u00a0echo\u00a0${test%%\/*}<\/pre>\n<p>${\u53d8\u91cf\u540d#substring\u6b63\u5219\u8868\u8fbe\u5f0f}\u4ece\u5b57\u7b26\u4e32\u5f00\u5934\u5f00\u59cb\u914d\u5907substring,\u5220\u9664\u5339\u914d\u4e0a\u7684\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>${\u53d8\u91cf\u540d%substring\u6b63\u5219\u8868\u8fbe\u5f0f}\u4ece\u5b57\u7b26\u4e32\u7ed3\u5c3e\u5f00\u59cb\u914d\u5907substring,\u5220\u9664\u5339\u914d\u4e0a\u7684\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>\u6ce8\u610f\uff1a${test##*\/},${test%\/*}\u00a0\u5206\u522b\u662f\u5f97\u5230\u6587\u4ef6\u540d\uff0c\u6216\u8005\u76ee\u5f55\u5730\u5740\u6700\u7b80\u5355\u65b9\u6cd5\u3002<\/p>\n<h6>4.\u5b57\u7b26\u4e32\u66ff\u6362<\/h6>\n<pre class=\"lang:default decode:true\">[chengmo@localhost\u00a0~]$\u00a0test='c:\/windows\/boot.ini'\n[chengmo@localhost\u00a0~]$\u00a0echo\u00a0${test\/\/\/\\}\nc:windows\/boot.ini\n[chengmo@localhost\u00a0~]$\u00a0echo\u00a0${test\/\/\/\/\\}\nc:windowsboot.ini<\/pre>\n<p>${\u53d8\u91cf\/\u67e5\u627e\/\u66ff\u6362\u503c}\u00a0\u4e00\u4e2a\u201c\/\u201d\u8868\u793a\u66ff\u6362\u7b2c\u4e00\u4e2a\uff0c\u201d\/\/\u201d\u8868\u793a\u66ff\u6362\u6240\u6709,\u5f53\u67e5\u627e\u4e2d\u51fa\u73b0\u4e86\uff1a\u201d\/\u201d\u8bf7\u52a0\u8f6c\u4e49\u7b26\u201d\/\u201d\u8868\u793a\u3002<\/p>\n<h6>\u4e09\u3001\u6027\u80fd\u6bd4\u8f83<\/h6>\n<p>\u5728shell\u4e2d\uff0c\u901a\u8fc7awk,sed,expr\u00a0\u7b49\u90fd\u53ef\u4ee5\u5b9e\u73b0\uff0c\u5b57\u7b26\u4e32\u4e0a\u8ff0\u64cd\u4f5c\u3002\u4e0b\u9762\u6211\u4eec\u8fdb\u884c\u6027\u80fd\u6bd4\u8f83\u3002<\/p>\n<pre class=\"lang:default decode:true\">[chengmo@localhost\u00a0~]$\u00a0test='c:\/windows\/boot.ini'\n[chengmo@localhost\u00a0~]$\u00a0time\u00a0for\u00a0i\u00a0in\u00a0$(seq\u00a010000);do\u00a0a=${#test};done\nreal\u00a0\u00a0\u00a0\u00a00m0.173s\nuser\u00a0\u00a0\u00a0\u00a00m0.139s\nsys\u00a0\u00a0\u00a0\u00a0\u00a00m0.004s\n\n[chengmo@localhost\u00a0~]$\u00a0time\u00a0for\u00a0i\u00a0in\u00a0$(seq\u00a010000);do\u00a0a=$(expr\u00a0length\u00a0$test);done\nreal\u00a0\u00a0\u00a0\u00a00m9.734s\nuser\u00a0\u00a0\u00a0\u00a00m1.628s\n...<\/pre>\n<p>\u901f\u5ea6\u76f8\u5dee\u4e0a\u767e\u500d\uff0c\u8c03\u7528\u5916\u90e8\u547d\u4ee4\u5904\u7406\uff0c\u4e0e\u5185\u7f6e\u64cd\u4f5c\u7b26\u6027\u80fd\u76f8\u5dee\u975e\u5e38\u5927\u3002\u5728shell\u7f16\u7a0b\u4e2d\uff0c\u5c3d\u91cf\u7528\u5185\u7f6e\u64cd\u4f5c\u7b26\u6216\u8005\u51fd\u6570\u5b8c\u6210\u3002\u4f7f\u7528awk,sed\u7c7b\u4f3c\u4f1a\u51fa\u73b0\u8fd9\u6837\u7ed3\u679c\u3002<\/p>\n<h6>\u539f\u6587\u5730\u5740\uff1a<\/h6>\n<p><a href=\"http:\/\/www.cnblogs.com\/chengmo\/archive\/2010\/10\/02\/1841355.html\" target=\"_blank\">http:\/\/www.cnblogs.com\/chengmo\/archive\/2010\/10\/02\/1841355.html<\/a><\/p>\n<\/div>\n<div style=\"color: #000000;\">\n<hr \/>\n<p>\u975e\u5e38\u7ecf\u5178\u7684Linux shell\u5b57\u7b26\u4e32\u64cd\u4f5c\u8bb2\u89e3\uff0c\u642c\u8fd0\u81f3\u6b64\u505a\u4e2a\u5907\u4efd\uff08\u5b66\u4e60\u4e4b\u540e\u7684\u8bb0\u5f55\uff09\u3002<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Linux \u7684\u5b57\u7b26\u4e32\u622a\u53d6\u7684\u516b\u79cd\u65b9\u6cd5\uff1a \u5047\u8bbe\u6709\u53d8\u91cf\uff1avar=http:\/\/www.hao.com\/123.htm [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7,12],"tags":[30,65],"class_list":["post-512","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","category-tools","tag-linux","tag-shell"],"views":4567,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=512"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/512\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}