{"id":2292,"date":"2012-11-15T22:58:26","date_gmt":"2012-11-15T14:58:26","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=2292"},"modified":"2012-11-16T17:04:55","modified_gmt":"2012-11-16T09:04:55","slug":"%e7%ae%80%e6%b4%81%e7%9a%84bash%e7%bc%96%e7%a8%8b%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/2292.html","title":{"rendered":"\u7b80\u6d01\u7684Bash\u7f16\u7a0b\u6280\u5de7"},"content":{"rendered":"<p><span style=\"font-size: small;\">\u4e0b\u9762\u8fd9\u51e0\u6761\u662f\u6211\u81ea\u5df1\u5728\u5199shell\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u6bd4\u8f83\u559c\u6b22\u7684\u51e0\u79cd\u5199\u6cd5\uff0c\u629b\u7816\u5f15\u7389\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">1) \u68c0\u67e5\u547d\u4ee4\u6267\u884c\u662f\u5426\u6210\u529f<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u7b2c\u4e00\u79cd\u5199\u6cd5\uff0c\u6bd4\u8f83\u5e38\u89c1\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\necho abcdee | grep -q abcd\r\n\r\nif &#x5B; $? -eq 0 ]; then\r\n    echo &quot;Found&quot;\r\nelse\r\n    echo &quot;Not found&quot;\r\nfi\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7b80\u6d01\u7684\u5199\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nif echo abcdee | grep -q abc; then\r\n    echo &quot;Found&quot;\r\nelse\r\n    echo &quot;Not found&quot;\r\nfi\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5f53\u7136\u4f60\u4e5f\u53ef\u4ee5\u4e0d\u8981if\/else,\u4e0d\u8fc7\u8fd9\u6837\u53ef\u8bfb\u6027\u6bd4\u8f83\u5dee:<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sun Nov 04 05:58 AM] &#x5B;kodango@devops] ~\/workspace\r\n$ echo abcdee | grep -q abc &amp;&amp; echo &quot;Found&quot; || echo &quot;Not found&quot;\r\nFound\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">2) \u5c06\u6807\u51c6\u8f93\u51fa\u4e0e\u6807\u51c6\u9519\u8bef\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\/dev\/null<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u7b2c\u4e00\u79cd\u5199\u6cd5\uff0c\u6bd4\u8f83\u5e38\u89c1\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ngrep &quot;abc&quot; test.txt 1&gt;\/dev\/null 2&gt;&amp;1\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5e38\u89c1\u7684\u9519\u8bef\u5199\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ngrep &quot;abc&quot; test.txt 2&gt;&amp;1 1&gt;\/dev\/null\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7b80\u6d01\u7684\u5199\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ngrep &quot;abc&quot; test.txt &amp;&gt; \/dev\/null\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">3) awk\u7684\u4f7f\u7528<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u4e3e\u4e00\u4e2a\u5b9e\u9645\u7684\u4f8b\u5b50\uff0c\u83b7\u53d6Xen DomU\u7684id\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5e38\u89c1\u7684\u5199\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo xm li | grep vm_name | awk '{print $2}'\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7b80\u6d01\u7684\u5199\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo xm li | awk '\/vm_name\/{print $2}'\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">4) \u5c06\u4e00\u4e2a\u6587\u672c\u7684\u6240\u6709\u884c\u7528\u9017\u53f7\u8fde\u63a5\u8d77\u6765<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5047\u8bbe\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\u6240\u793a:<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 10:04 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ cat \/tmp\/test.txt\r\n1\r\n2\r\n3\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4f7f\u7528Sed\u547d\u4ee4\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 10:14 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ sed ':a;$!N;s\/\\n\/,\/;ta' \/tmp\/test.txt\r\n1,2,3\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7b80\u6d01\u7684\u5199\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 10:04 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ paste -sd, \/tmp\/test.txt\r\n1,2,3\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">5) \u8fc7\u6ee4\u91cd\u590d\u884c<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5047\u8bbe\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\u6240\u793a\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 10:16 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ sort \/tmp\/test.txt\r\n1\r\n1\r\n2\r\n3\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 10:16 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ sort \/tmp\/test.txt | uniq\r\n1\r\n2\r\n3\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7b80\u5355\u7684\u5199\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 10:16 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ sort \/tmp\/test.txt -u\r\n1\r\n2\r\n3\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">6) grep\u67e5\u627e\u5355\u8bcd<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5047\u8bbe\u4e00\u4e2a\u6587\u672c\u7684\u6bcf\u4e00\u884c\u662f\u4e00\u4e2aip\u5730\u5740\uff0c\u4f8b\u5982<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 10:20 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ cat \/tmp\/ip.list\r\n10.0.0.1\r\n10.0.0.12\r\n10.0.0.123\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4f7f\u7528grep\u67e5\u627e\u662f\u5426\u5305\u62ec10.0.0.1\u8fd9\u4e2aip\u5730\u5740\u3002\u5e38\u89c1\u7684\u5199\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 10:22 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ grep '10.0.0.1\\&gt;' \/tmp\/ip.list\r\n10.0.0.1\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7b80\u5355\u7684\u65b9\u6cd5\uff08\u5176\u5b9e\u8fd9\u65b9\u6cd5\u4e0d\u89c1\u5f97\u7b80\u5355\uff0c\u53ea\u662f\u4e3a\u4e86\u8bf4\u660e-w\u8fd9\u4e2a\u53c2\u6570\u8fd8\u662f\u5f88\u6709\u7528\u7684)<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 10:23 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ grep -w '10.0.0.1' \/tmp\/ip.list\r\n10.0.0.1\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u987a\u4fbfgrep\u7684-n\/-H\/-v\/-f\/-c\u8fd9\u51e0\u53c2\u6570\u90fd\u5f88\u6709\u7528\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">7) \u4e34\u65f6\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5e38\u89c1\u7684\u5199\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 10:26 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ export LC_ALL=zh_CN.UTF-8\r\n\r\n&#x5B;\u516d 11\u6708 03 10:26 \u4e0b\u5348] &#x5B;kodango@devops] ~\/workspace\r\n$ date\r\n2012\u5e74 11\u6708 03\u65e5 \u661f\u671f\u516d 22:26:55 CST\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7b80\u6d01\u7684\u5199\u6cd5:<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;\u516d 11\u6708 03 10:26 \u4e0b\u5348] &#x5B;kodango@devops] ~\/workspace\r\n$ unset LC_ALL\r\n\r\n&#x5B;Sat Nov 03 10:27 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ LC_ALL=zh_CN.UTF-8 date\r\n2012\u5e74 11\u6708 03\u65e5 \u661f\u671f\u516d 22:27:43 CST\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5728\u547d\u4ee4\u4e4b\u524d\u52a0\u4e0a\u73af\u5883\u53d8\u66f4\u7684\u8bbe\u7f6e\uff0c\u53ea\u662f\u4e34\u65f6\u6539\u53d8\u5f53\u524d\u6267\u884c\u547d\u4ee4\u7684\u73af\u5883\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">8) $1,$2&#8230;\u7b49\u4f4d\u7f6e\u53c2\u6570\u7684\u4f7f\u7528<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5047\u8bbe\u53ea\u60f3\u4f7f\u7528$2,$3..\u8fd9\u51e0\u4e2a\u53c2\u6570\uff0c\u5e38\u89c1\u7684\u505a\u6cd5\u662f:<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nshift\r\necho &quot;$@&quot;\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4e3a\u4ec0\u4e48\u4e0d\u8fd9\u6837\u5199\u5462\uff1f<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\necho &quot;${@:2}&quot;\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">9\uff09\u9000\u800c\u6c42\u5176\u6b21\u7684\u5199\u6cd5<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u76f8\u4fe1\u5927\u5bb6\u4f1a\u6709\u8fd9\u79cd\u9700\u6c42\uff0c\u5f53\u4e00\u4e2a\u53c2\u6570\u503c\u6ca1\u6709\u63d0\u4f9b\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002\u5e38\u89c1\u7684\u5199\u6cd5\u662f\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\narg=$1\r\n\r\nif &#x5B; -z &quot;$arg&quot; ]; then\r\n   arg=0\r\nfi\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7b80\u6d01\u7684\u5199\u6cd5\u662f\u8fd9\u6837\u7684:<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\narg=${1:-0}\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">10\uff09bash\u7279\u6b8a\u53c2\u6570&#8211;\u7684\u7528\u6cd5<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5047\u8bbe\u8981\u7528grep\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b-i\uff0c\u6211\u4eec\u4f1a\u8fd9\u6837\u5c1d\u8bd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 10:45 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ echo 'abc-i' | grep &quot;-i&quot;\r\nUsage: grep &#x5B;OPTION]... PATTERN &#x5B;FILE]...\r\nTry 'grep --help' for more information.\r\n\r\n&#x5B;Sat Nov 03 10:45 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ echo 'abc-i' | grep &quot;\\-i&quot;\r\nabc-i\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7b80\u6d01\u7684\u65b9\u6cd5\u662f\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 10:45 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ echo 'abc-i' | grep -- -i\r\nabc-i\r\n<\/pre>\n<p><span style=\"font-size: small;\">bash\u4e2d&#8211;\u540e\u9762\u7684\u53c2\u6570\u4e0d\u4f1a\u88ab\u5f53\u4f5c\u9009\u9879\u89e3\u6790\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">11\uff09\u51fd\u6570\u7684\u8fd4\u56de\u503c\u9ed8\u8ba4\u662f\u6700\u540e\u4e00\u884c\u8bed\u53e5\u7684\u8fd4\u56de\u503c<\/span><\/strong><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n# Check whether an item is a function\r\n# $1: the function name\r\n# Return: 0(yes) or 1(no)\r\nfunction is_function()\r\n{\r\n    local func_name=$1\r\n    test &quot;`type -t $1 2&gt;\/dev\/null`&quot; = &quot;function&quot;\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4e0d\u8981\u753b\u86c7\u6dfb\u8db3\u518d\u5728\u540e\u9762\u52a0\u4e00\u884creturn $?\u4e86\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">12) \u5c06printf\u683c\u5f0f\u5316\u7684\u7ed3\u679c\u8d4b\u503c\u7ed9\u53d8\u91cf<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u4f8b\u5982\u5c06\u6570\u5b57\u8f6c\u6362\u6210\u5176\u5341\u516d\u8fdb\u5236\u5f62\u5f0f\uff0c\u5e38\u89c1\u7684\u5199\u6cd5\u662f\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 10:55 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ var=$(printf '%%%02x' 111)\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7b80\u5355\u7684\u5199\u6cd5\u662f\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 10:54 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ printf -v var '%%%02x' 111\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u770b\u770bprintf\u7684help<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 10:53 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ help printf | grep -A 1 -B 1 -- -v\r\nprintf: printf &#x5B;-v var] format &#x5B;arguments]\r\n    Formats and prints ARGUMENTS under control of the FORMAT.\r\n--\r\n    Options:\r\n      -v var\tassign the output to shell variable VAR rather than\r\n    \t\tdisplay it on the standard output\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">13\uff09\u6253\u5370\u6587\u4ef6\u884c<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u6253\u5370\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nhead -1 test.txt\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6253\u5370\u6587\u4ef6\u7684\u7b2c2\u884c\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsed -n '2p' test.txt\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6253\u5370\u6587\u4ef6\u7684\u7b2c2\u52305\u884c\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsed -n '2,5p' test.txt\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6253\u5370\u6587\u4ef6\u7684\u7b2c2\u884c\u59cb\uff08\u5305\u62ec\u7b2c2\u884c\u5728\u5185\uff095\u884c\u7684\u5185\u5bb9\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsed -n '2,+4p' test.txt\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6253\u5370\u5012\u6570\u7b2c\u4e8c\u884c\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ tail -2 test.txt | head -1\r\n$ tac test.txt | sed -n '2p'\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">14\uff09\u5584\u7528let\u6216\u8005(())\u547d\u4ee4\u505a\u7b97\u672f\u8fd0\u7b97<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5982\u4f55\u5bf9\u4e00\u4e2a\u6570\u5b57\u505a++\u8fd0\u7b97\uff0c\u53ef\u80fd\u4f60\u4f1a\u8fd9\u6837\u7528\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\na=1\r\na=`expr a + 1`\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4e3a\u4f55\u4e0d\u7528\u4f60\u719f\u6089\u7684:<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\na=1\r\nlet a++\r\nlet a+=2\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">15\uff09\u83b7\u53d6\u8f6f\u8fde\u63a5\u6307\u5b9a\u7684\u771f\u5b9e\u6587\u4ef6\u540d<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5982\u679c\u4f60\u4e0d\u77e5\u9053\uff0c\u4f60\u53ef\u80fd\u4f1a\u8fd9\u6837\u83b7\u53d6\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 11:12 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ ls -l \/usr\/bin\/python | awk -F'-&gt;' '{print $2}' | tr -d ' '\r\n\/usr\/bin\/python2\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5982\u679c\u4f60\u77e5\u9053\u6709\u4e00\u4e2a\u53ebreadlink\u7684\u547d\u4ee4\uff0c\u90a3\u4e48\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 11:13 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ readlink \/usr\/bin\/python\r\n\/usr\/bin\/python2\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">16\uff09\u83b7\u53d6\u4e00\u4e2a\u5b57\u7b26\u7684ASCII\u7801<\/span><\/strong><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 03 11:14 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ printf '%02x' &quot;'+&quot;\r\n2b\r\n&#x5B;Sat Nov 03 11:30 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ echo -n '+' | od -tx1 -An | tr -d ' '\r\n2b\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">17\uff09\u6e05\u7a7a\u4e00\u4e2a\u6587\u4ef6<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5e38\u89c1\u7684\u7528\u6cd5:<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\necho &quot;&quot; &gt; test.txt\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7b80\u5355\u7684\u5199\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&gt; test.txt\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">18) \u4e0d\u8981\u5fd8\u8bb0\u6709here document<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u4e0b\u9762\u4e00\u6bb5\u4ee3\u7801\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ngrep -v 1 \/tmp\/test.txt | while read line; do\r\n    let a++\r\n    echo --$line--\r\ndone\r\n\r\necho a:$a\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6267\u884c\u540e\u6709\u4ec0\u4e48\u95ee\u9898\u5417\uff1f<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sun Nov 04 05:35 AM] &#x5B;kodango@devops] ~\/workspace\r\n$ sh test.sh\r\n--2--\r\n--3--\r\na:\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u53d1\u73b0a\u8fd9\u4e2a\u53d8\u91cf\u6ca1\u6709\u88ab\u8d4b\u503c\uff0c\u4e3a\u4ec0\u4e48\u5462\uff1f\u56e0\u4e3a\u7ba1\u9053\u540e\u9762\u7684\u4ee3\u7801\u662f\u5728\u5728\u4e00\u4e2a\u5b50shell\u4e2d\u6267\u884c\u7684\uff0c\u6240\u505a\u7684\u4efb\u4f55\u66f4\u6539\u90fd\u4e0d\u4f1a\u5bf9\u5f53\u524dshell\u6709\u5f71\u54cd\uff0c\u81ea\u7136a\u8fd9\u4e2a\u53d8\u91cf\u5c31\u4e0d\u4f1a\u6709\u8d4b\u503c\u4e86\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u6362\u4e00\u79cd\u601d\u8def\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ngrep -v 1 \/tmp\/test.txt &gt; \/tmp\/test.tmp\r\n\r\nwhile read line; do\r\n    let a++\r\n    echo --$line--\r\ndone &lt; \/tmp\/test.tmp\r\n\r\necho a:$a\r\nrm -f \/tmp\/test.tmp\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4e0d\u8fc7\u591a\u4e86\u4e00\u4e2a\u4e34\u65f6\u6587\u4ef6\uff0c\u6700\u540e\u8fd8\u8981\u5220\u9664\u3002\u8fd9\u91cc\u5176\u5b9e\u53ef\u4ee5\u7528\u5230here document\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nb=1\r\nwhile read line2; do\r\n    let b++\r\n    echo ??$line2??\r\ndone &lt; &lt; EOF\r\n`grep -v 1 \/tmp\/test.txt`\r\nEOF\r\n\r\necho b: $b\r\n<\/pre>\n<p><span style=\"font-size: small;\">here document\u5f80\u5f80\u7528\u4e8e\u9700\u8981\u8f93\u51fa\u4e00\u5927\u6bb5\u6587\u672c\u7684\u5730\u65b9\uff0c\u4f8b\u5982\u811a\u672c\u7684help\u51fd\u6570\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">19\uff09\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u6216\u8005\u6700\u540e\u4e00\u4e2a\u5b57\u7b26<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5047\u8bbe\u5b57\u7b26\u4e32\u4e3a\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sun Nov 04 10:21 AM] &#x5B;kodango@devops] ~\/workspace\r\n$ str=&quot;aremoveb&quot;\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u53ef\u80fd\u4f60\u7b2c\u4e00\u4e2a\u60f3\u6cd5\u662f\u901a\u8fc7sed\u6216\u8005\u5176\u5b83\u547d\u4ee4\u6765\u5b8c\u6210\u8fd9\u4e2a\u529f\u80fd\uff0c\u4f46\u662f\u5176\u5b9e\u6709\u5f88\u7b80\u5355\u7684\u65b9\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sun Nov 04 10:24 AM] &#x5B;kodango@devops] ~\/workspace\r\n$ echo &quot;${str#?}&quot;\r\nremoveb\r\n\r\n&#x5B;Sun Nov 04 10:24 AM] &#x5B;kodango@devops] ~\/workspace\r\n$ echo &quot;${str%?}&quot;\r\naremove\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7c7b\u4f3c\u5730\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5220\u96642\u4e2a\u30013\u4e2a\u30014\u4e2a\u2026\u2026<\/span><\/p>\n<p><span style=\"font-size: small;\">\u6709\u6ca1\u6709\u4e00\u6b21\u6027\u5220\u9664\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u7684\u65b9\u6cd5\u5462\uff1f\u7b54\u6848\u5f53\u7136\u662f\u80af\u5b9a\u7684\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sun Nov 04 10:26 AM] &#x5B;kodango@devops] ~\/workspace\r\n$ echo &quot;${str:1:-1}&quot;\r\nremove\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5173\u4e8e\u8fd9\u4e9b\u53d8\u91cf\u66ff\u6362\u7684\u5185\u5bb9\u5728bash\u7684man\u624b\u518c\u4e2d\u90fd\u6709\u8bf4\u660e\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">20\uff09\u4f7f\u7528\u9017\u53f7join\u6570\u7ec4\u5143\u7d20<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5047\u8bbe\u6570\u7ec4\u5143\u7d20\u6ca1\u6709\u7a7a\u683c\uff0c\u53ef\u4ee5\u7528\u8fd9\u79cd\u65b9\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sun Nov 04 10:14 AM] &#x5B;kodango@devops] ~\/workspace\r\n$ a=(1 2 3)\r\n$ b=&quot;${a&#x5B;*]}&quot;\r\n\r\n&#x5B;Sun Nov 04 10:15 AM] &#x5B;kodango@devops] ~\/workspace\r\n$ echo ${b\/\/ \/,}\r\n1,2,3\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6ce8\u610f\uff1a\u5f53\u8be5\u6570\u7ec4\u7684\u957f\u5ea6\u975e\u5e38\u957f\u65f6\uff0c\u4f7f\u7528\u8fd9\u79cd\u66ff\u6362\u7684\u65f6\u95f4\u5f00\u9500\u5f88\u9ad8\uff0c\u6027\u80fd\u5f88\u5dee\uff0c\u63a8\u8350\u7528sed\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5047\u8bbe\u6570\u7ec4\u5143\u7d20\u5305\u542b\u6709\u7a7a\u683c\uff0c\u53ef\u4ee5\u501f\u7528printf\u547d\u4ee4\u6765\u8fbe\u5230\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sun Nov 04 10:15 AM] &#x5B;kodango@devops] ~\/workspace\r\n$ a=(1 &quot;2 3&quot; 4)\r\n\r\n&#x5B;Sun Nov 04 10:15 AM] &#x5B;kodango@devops] ~\/workspace\r\n$ printf &quot;,%s&quot; &quot;${a&#x5B;@]}&quot; | cut -c2-\r\n1,2 3,4\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">21) Shell\u4e2d\u7684\u591a\u8fdb\u7a0b<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5728\u547d\u4ee4\u884c\u4e0b\uff0c\u6211\u4eec\u4f1a\u5728\u547d\u4ee4\u884c\u540e\u9762\u52a0\u4e0a&amp;\u7b26\u53f7\u6765\u8ba9\u8be5\u547d\u4ee4\u5728\u540e\u53f0\u6267\u884c\uff0c\u5728shell\u811a\u672c\u4e2d\uff0c\u4f7f\u7528&#8221;(cmd)&#8221;\u53ef\u4ee5\u8ba9fork\u4e00\u4e2a\u5b50shell\u6765\u6267\u884c\u8be5\u547d\u4ee4\u3002\u5229\u7528\u8fd9\u4e24\u70b9\uff0c\u53ef\u4ee5\u5b9e\u73b0shell\u7684\u591a\u7ebf\u7a0b\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\njob_num=10\r\n\r\nfunction do_work()\r\n{\r\n    echo &quot;Do work..&quot;\r\n}\r\n\r\nfor ((i=0; i&lt;job_num ;i++)); do\r\n    echo &quot;Fork job $i&quot;\r\n    (do_work) &amp;\r\ndone\r\n\r\nwait   # wait for all job done\r\necho &quot;All job have been done!&quot;\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6ce8\u610f\u6700\u540e\u7684wait\u547d\u4ee4\uff0c\u4f5c\u7528\u662f\u7b49\u5f85\u6240\u6709\u5b50\u8fdb\u7a0b\u7ed3\u675f\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">22\uff09 bash\u4e2dalias\u7684\u4f7f\u7528<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">alias\u5176\u5b9e\u662f\u7ed9\u5e38\u7528\u7684\u547d\u4ee4\u5b9a\u4e00\u4e2a\u522b\u540d\uff0c\u6bd4\u5982\u5f88\u591a\u4eba\u4f1a\u5b9a\u4e49\u4e00\u4e0b\u7684\u4e00\u4e2a\u522b\u540d\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nalias ll='ls -l'\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4ee5\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528ll\uff0c\u5b9e\u9645\u5c55\u5f00\u540e\u6267\u884c\u7684\u662fls -l\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u73b0\u5728\u5f88\u591a\u53d1\u884c\u7248\u90fd\u4f1a\u5e26\u51e0\u4e2a\u9ed8\u8ba4\u7684\u522b\u540d\uff0c\u6bd4\u5982\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nalias grep='grep --color=auto'  # \u5e26\u989c\u8272\u663e\u793a\r\nalias ls='ls --color=auto' # \u540c\u4e0a\r\nalias rm='rm -i'  # \u5220\u9664\u6587\u4ef6\u9700\u8981\u786e\u8ba4\r\n<\/pre>\n<p><span style=\"font-size: small;\">alias\u5728\u67d0\u4e9b\u65b9\u9762\u786e\u5b9e\u63d0\u9ad8\u4e86\u5f88\u5927\u7684\u6548\u7387\uff0c\u4f46\u662f\u4e5f\u662f\u6709\u9690\u60a3\u7684\uff0c\u8fd9\u70b9\u53ef\u4ee5\u770b\u6211\u4ee5\u524d\u7684\u4e00\u7bc7\u6587\u7ae0<a href=\"http:\/\/kodango.me\/hidden-text-in-terminal\">\u7ec8\u7aef\u4e0b\u8089\u773c\u770b\u4e0d\u89c1\u7684\u4e1c\u897f<\/a>\u3002\u90a3\u4e48\u5982\u4f55\u4e0d\u8981\u5c55\u5f00alias\uff0c\u800c\u662f\u7528\u672c\u6765\u7684\u610f\u601d\u5462\uff1f\u7b54\u6848\u662f\u4f7f\u7528\u8f6c\u4e49\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n\\ls\r\n\\grep\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5728\u547d\u4ee4\u524d\u9762\u52a0\u4e00\u4e2a\u53cd\u659c\u6760\u540e\u5c31\u53ef\u4ee5\u4e86\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u8fd9\u91cc\u8981\u63d2\u4e00\u6bb5\u6545\u4e8b\uff0c\u524d\u4e24\u5929\u6211\u5728shell\u811a\u672c\u4e2d\u5b9a\u4e49\u4e86\u4e0b\u9762\u7684\u4e00\u4e2aalias\uff0c\u5047\u8bbe\u4f4d\u4e8e\u6587\u4ef6util.sh\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n...\r\nalias ssh='ssh -o StrictHostKeyChecking=no -o LogLevel=quiet -o BatchMode=yes'\r\n...\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u540e\u9762\u8fd9\u4e32ssh\u9009\u9879\u662f\u4e3a\u4e86\u53bb\u6389\u4e00\u4e9bwarning\u7684\u4fe1\u606f\uff0c\u4e0d\u63d0\u793a\u8f93\u5165\u5bc6\u7801\u7b49\u7b49\u3002\u5177\u4f53\u53ef\u4ee5\u770bssh\u7684\u6587\u6863\u8bf4\u660e\u3002\u6211\u81ea\u5df1\u6d4b\u8bd5\u7684\u65f6\u5019\u597d\u597d\u7684\uff0c\u5f53\u65f6\u6211\u540c\u4e8b\u8dd1\u5f97\u65f6\u5019\u5374\u4f9d\u7136\u6709\u62a5Warning\u3002\u6211\u5bf9\u6bd4\u4e86\u4e0b\u6211\u4eec\u4e24\u4e2a\u4eba\u7684\u7528\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsh util.sh  # \u6211\u7684\r\n.\/util.sh   # \u4ed6\u7684\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5927\u5bb6\u5e94\u8be5\u77e5\u9053\uff0c\u76f4\u63a5.\/util.sh\u6267\u884c\uff0cshell\u4f1a\u53bb\u627e\u811a\u672c\u7b2c\u4e00\u884c\u7684shebang\u4e2d\u7ed9\u5b9a\u7684\u89e3\u91ca\u5668\u53bb\u6267\u884c\u6539\u811a\u672c\uff0c\u6240\u4ee5\u7b2c\u4e8c\u79cd\u7528\u6cd5\u76f8\u5f53\u4e8e\u76f4\u63a5\u7528bash\u6765\u6267\u884c\u3002\u90a3\u60f3\u5fc5\u662fbash\/sh\u5bf9alias\u662f\u5426\u9ed8\u8ba4\u5c55\u5f00\u8fd9\u4e00\u70b9\u4e0a\u662f\u6709\u533a\u522b\u7684\u4e86\uff08\u53ef\u80fd\u662fbash\u7248\u672c\u7684\u95ee\u9898\uff0cRHEL 5U4\uff09\u3002\u7ffb\u9605\u4e86\u4e0bBash\u7684man\u624b\u518c\uff0c\u53d1\u73b0\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eexpand_aliases\u9009\u9879\u6765\u6253\u5f00alias\u5c55\u5f00\u7684\u529f\u80fd\uff0c\u9ed8\u8ba4\u5728\u975e\u4ea4\u4e92\u5f0fShell\u4e0b\u662f\u5173\u95ed\u7684\uff08<a href=\"http:\/\/kodango.me\/what-is-interactive-and-login-shell\">\u4ec0\u4e48\u662f\u4ea4\u4e92\u5f0f\u767b\u5f55Shell<\/a>\uff09\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u4fee\u6539\u4e0butil.sh\uff0c\u6253\u5f00\u8fd9\u4e2a\u9009\u9879\u5c31Ok\u4e86\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n...\r\n# Expand aliases in script\r\nshopt -s expand_aliases\r\nalias ssh='ssh -o StrictHostKeyChecking=no -o LogLevel=quiet -o BatchMode=yes'\r\n...\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">23\uff09awk\u6253\u5370\u9664\u7b2c\u4e00\u5217\u4e4b\u5916\u7684\u5176\u4ed6\u5217<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">awk\u7528\u6765\u622a\u53d6\u8f93\u5165\u884c\u4e2d\u7684\u67d0\u51e0\u5217\u5f88\u6709\u7528\uff0c\u5f53\u65f6\u5982\u679c\u8981\u6392\u9664\u67d0\u51e0\u5217\u5462\uff1f<\/span><br \/>\n<span style=\"font-size: small;\"> \u4f8b\u5982\u6709\u5982\u4e0b\u7684\u4e00\u4e2a\u6587\u4ef6\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ cat \/tmp\/test.txt\r\n1 2 3 4 5\r\n10 20 30 40 50\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u89e3\u51b3\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ awk '{$1=&quot;&quot;;print $0}' \/tmp\/test.txt\r\n 2 3 4 5\r\n 20 30 40 50\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4f46\u662f\u524d\u9762\u591a\u4e86\u4e00\u4e2a\u7a7a\u683c\uff0c\u53ef\u4ee5\u7528cut\u547d\u4ee4\u7a0d\u5fae\u8c03\u6574\u4e0b\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ awk '{$1=&quot;&quot;;print $0}' \/tmp\/test.txt | cut -c2-\r\n2 3 4 5\r\n20 30 40 50\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">24\uff09 \u5de7\u7528bash\u7684\u547d\u4ee4\u5c55\u5f00\u529f\u80fd\u5907\u4efd\u6587\u4ef6<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5047\u8bbe\u8981\u5907\u4efd\u6587\u4ef6\/your\/path\/to\/file.list\u4e3a\/your\/path\/to\/file.list.20121106\uff0c\u5e38\u89c4\u7684\u65b9\u6cd5\u662f\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncp \/your\/path\/to\/file.list \/your\/path\/to\/file.list.20121106\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8fd9\u6837\u91cd\u590d\u5199\u4e0a\u4e00\u957f\u4e32\u7684\u8def\u5f84\uff0c\u662f\u4e0d\u662f\u5f88\u9ebb\u70e6\uff0c\u8fd9\u91cc\u5229\u7528bash\u7684\u5c55\u5f00\u7279\u6027\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncp \/your\/path\/to\/file.list{,.20121106}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\/your\/path\/to\/file.list{,.20121106}\u8fd9\u4e00\u90e8\u5206\u4f1a\u5c55\u5f00\u4e3a\/your\/path\/to\/file.list \/your\/path\/to\/file.list.20121106,\u518d\u5c06\u6b64\u4f20\u7ed9cp\u547d\u4ee4\uff0c\u5c31\u8fbe\u5230\u4e86\u4e0e\u524d\u9762\u540c\u6837\u7684\u6548\u679c\u3002\uff08\u601d\u8def\u540cls *\uff09\u3002\u5177\u4f53\u53ef\u4ee5man bash\u4e2d\u7684Brace Expansion\u8fd9\u4e00\u6bb5\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">25\uff09 \u547d\u4ee4\u884c\u4e0b\u4f7f\u7528ctrl+x ctrl+e\u6765\u7f16\u8f91\u5f53\u524d\u547d\u4ee4<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u8fd9\u4e2a\u6280\u5de7\u6765\u81ea<a href=\"http:\/\/wowubuntu.com\/linux_shell_2.html\">\u6700\u725bB\u7684 Linux Shell \u547d\u4ee4\u7cfb\u5217\u8fde\u8f7d\uff08\u4e8c\uff09<\/a>\u3002\u4f7f\u7528\u65b9\u6cd5\u662f\u952e\u5165\u547d\u4ee4\u4e4b\u540e\uff0c\u518d\u6309ctrl+x ctrl+e\u53ef\u4ee5\u6253\u5f00\u4e00\u4e2a\u7f16\u8f91\u5668\u6765\u7f16\u8f91\u547d\u4ee4\uff0c\u9ed8\u8ba4\u662f\u4f7f\u7528emacs\u3002\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5728~\/.bashrc\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u8fd9\u4e00\u884c\uff0c\u5c06\u7f16\u8f91\u5668\u6362\u6210vim:<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nexport EDITOR='vim'\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4e3a\u4ec0\u4e48\u63a8\u8350\u8fd9\u4e00\u6761\u5462\uff1f\u5bf9\u4e8e\u4e00\u822c\u7684\u547d\u4ee4\uff08\u8fd9\u91cc\u6307\u7684\u662f\u957f\u5ea6\u5f88\u77ed\u7684\u547d\u4ee4\uff09\u5176\u5b9e\u8fd9\u4e2a\u6280\u5de7\u6ca1\u4ec0\u4e48\u7528\u5904\uff0c\u6211\u7528\u65b9\u5411\u952e\u79fb\u4e00\u4e0b\u5c31OK\u4e86\uff0c\u4f46\u662f\u6709\u65f6\u5019\uff08\u5c24\u5176\u662f\u8fd0\u7ef4\u7684\u4e00\u4e9b\u547d\u4ee4\uff09\u6709\u4e9b\u547d\u4ee4\u957f\u5ea6\u7279\u522b\u957f\uff0c\u4e00\u5806\u53c2\u6570\uff0c\u5982\u679c\u76f4\u63a5\u5728\u547d\u4ee4\u884c\u4fee\u6539\u5176\u5b9e\u98ce\u9669\u5f88\u9ad8\u7684\uff08\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u7684\u5f00\u5934\u52a0\u4e0a\u4e00\u4e2a#\u53f7\u6765\u89c4\u907f\u8fd9\u4e2a\u98ce\u9669\uff0cBash\u5c06\u5f53\u524d\u7684\u547d\u4ee4\u5f53\u6210\u6ce8\u91ca\u4e0d\u6267\u884c\uff09\uff0c\u800c\u4e14\u65b9\u5411\u952e\u4e00\u4e2a\u4e00\u4e2a\u8fc1\u79fb\u975e\u5e38\u4e0d\u65b9\u4fbf\uff08\u5f53\u7136\u6709\u7c7b\u4f3cctrl+x ctrl+e\u8fd9\u79cd\u9884\u8bbe\u7684\u5feb\u6377\u952e\u6765\u64cd\u4f5c\uff0c\u53ef\u4ee5\u770bbind -p)\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u50cf\u4f7f\u7528ctrl+x,ctrl+e\u6253\u5f00vim\u6765\u7f16\u8f91\u547d\u4ee4\u5728\u8fd9\u79cd\u573a\u666f\u6709\u4e24\u79cd\u597d\u5904\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\">a. \u53ef\u4ee5\u65b9\u4fbf\u7684\u7528\u719f\u6089\u7684\u7f16\u8f91\u5668\u9ad8\u6548\u5730\u4fee\u6539\u547d\u4ee4\uff1b<\/span><br \/>\n<span style=\"font-size: small;\"> b. \u6709\u4e00\u4e2a\u786e\u8ba4\u7684\u8fc7\u7a0b\uff0c\u65e0\u8bef\u540e\uff0c\u9000\u51favim\u624d\u6267\u884c\u547d\u4ee4\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e0d\u8fc7\u6211\u4e0d\u662f\u5f88\u63a8\u8350<a href=\"http:\/\/wowubuntu.com\/tag\/linuxshell\">\u6700\u725bB\u7684 Linux Shell \u547d\u4ee4<\/a> \u7cfb\u5217\u8fde\u8f7d\u4e2d\u7684\u4e00\u4e9b\u5bf9\u5386\u53f2\u547d\u4ee4\u7684\u6280\u5de7\uff0c\u867d\u7136\u65b9\u4fbf\uff0c\u4f46\u662f\u98ce\u9669\u5f88\u9ad8\uff0c\u56e0\u4e3a\u6ca1\u6709\u4e00\u4e2a\u786e\u8ba4\u7684\u8fc7\u7a0b\uff0c\u662f\u6267\u884c\u5c06\u5386\u53f2\u547d\u4ee4\u8c03\u51fa\u5c31\u6267\u884c\u4e86\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">26\uff09 \u4f60\u77e5\u9053sed\u7684\u8fd9\u4e2a\u7279\u6027\u5417\uff1f<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5047\u8bbe\u4e00\u4e2a\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\u4e3a\u4e00\u4e2a\u8def\u5f84\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Tue Nov 06 06:33 PM] &#x5B;kodango@devops] ~\r\n$ cat \/tmp\/test.txt\r\n\/home\/kodango\/hello\r\n\/home\/kodango\/hello\/world\r\n\/home\/kodango\/good\r\n\/home\/kodango\/good\/bye\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u73b0\u5728\u8981\u628a\/home\/kodango\/good\u66ff\u6362\u6210\/home\/kodango\/bad\uff0c\u666e\u901a\u7684\u4f5c\u6cd5\u662f\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Tue Nov 06 06:35 PM] &#x5B;kodango@devops] ~\r\n$ sed -n 's\/\\\/home\\\/kodango\\\/good\/\\\/home\\\/kodango\\\/bye\/p' \/tmp\/test.txt\r\n\/home\/kodango\/bye\r\n\/home\/kodango\/bye\/bye\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u56e0\u4e3a\u8def\u5f84\u4e2d\u7684\u5206\u9694\u7b26\u4e0esed\u7684\u66ff\u6362\u547d\u4ee4\u7684\u5206\u9694\u7b26\u90fd\u662f&#8217;\/&#8217;\uff0c\u6240\u4ee5\u9700\u8981\u8f6c\u4e49\uff0c\u975e\u5e38\u9ebb\u70e6\u3002\u5e78\u8fd0\u7684\u662f\uff0csed\u53ef\u4ee5\u66f4\u6539\u5206\u9694\u7b26\uff0c\u4f8b\u5982\u4f7f\u7528#\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Tue Nov 06 06:34 PM] &#x5B;kodango@devops] ~\r\n$ sed -n 's#\/home\/kodango\/good#\/home\/kodango\/bad#p' \/tmp\/test.txt\r\n\/home\/kodango\/bad\r\n\/home\/kodango\/bad\/bye\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8fd9\u6837\u5c31\u6e05\u723d\u591a\u4e86\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u8865\u5145\uff0c\u5982\u679c\u662f\u5728\u5730\u5740\u5bf9\u4e2d\u4f7f\u7528\uff0c\u9996\u4e2a\u5206\u9694\u7b26\u524d\u9762\u8981\u52a0\u53cd\u659c\u6760\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ sed -n '\\#\/home\/kodango\/#p' \/tmp\/test.txt\r\n\/home\/kodango\/hello\r\n\/home\/kodango\/hello\/world\r\n\/home\/kodango\/good\r\n\/home\/kodango\/good\/bye\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u53c2\u89c1\uff1a<a href=\"http:\/\/backreference.org\/2010\/02\/20\/using-different-delimiters-in-sed\/\">Using different delimiters in sed<\/a>\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">27\uff09 \u5408\u5e76\u8fde\u7eed\u91cd\u590d\u7684\u5b57\u7b26\uff08\u5373squeeze\u64cd\u4f5c\uff09<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u4f8b\u5982\u8981\u5408\u5e76\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u8fde\u7eed\u7684\u591a\u4e2a\u7a7a\u683c\uff0c\u5047\u8bbe\u5b57\u7b26\u4e32\u4e3a&#8217;print hello, world&#8217;\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u7b2c\u4e00\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528sed\u547d\u4ee4\uff0c\u626b\u63cf\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c\u66ff\u63622\u4e2a\u4ee5\u4e0a\u7684\u7a7a\u683c\u4e3a1\u683c\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ echo 'print  hello,   world  ' | sed -r 's\/ {2,}\/ \/g'\r\nprint hello, world\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528tr\u547d\u4ee4\u7684-s\u9009\u9879\uff0c\u4e13\u95e8\u5c31\u662f\u4e3a\u4e86\u5408\u5e76\u8fde\u7eed\u91cd\u590d\u7684\u5b57\u7b26\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ echo 'print  hello,   world  ' | tr -s ' '\r\nprint hello, world\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7b2c\u4e09\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528awk\u7684\u57df\u8d4b\u503c\u6765\u5b8c\u6210\u8be5\u76ee\u7684\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ echo 'print  hello,   world  ' | awk '$1=$1'\r\nprint hello, world\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5bf9\u5df2\u7ecf\u5b58\u5728\u7684\u57df\u4f8b\u5982$1,$2..\u8fdb\u884c\u8d4b\u503c\uff0c\u4f1a\u5bfc\u81f4awk\u91cd\u65b0\u4f7f\u7528OFS\u8f93\u51fa\u5206\u9694\u7b26\u91cd\u7ec4$0\uff0c\u5173\u4e8e\u8fd9\u4e00\u70b9\u7684\u8be6\u7ec6\u8bf4\u660e\u89c1sosodream\u540c\u5b66\u7684\u535a\u6587<a href=\"http:\/\/blog.csdn.net\/sosodream\/article\/details\/6425192\">Awk\u91cc\u7684\u57df\u8d4b\u503c\u64cd\u4f5c\u548c\u90e8\u5206\u6e90\u7801\u89e3\u6790\uff08$1=$1,$0=$0,FS,OFS)<\/a><\/span><\/p>\n<p><strong><span style=\"font-size: small;\">28\uff09 \u5c06\u6587\u672c\u4e2d\u67d0\u5217\u76f8\u540c\u7684\u884c\u8f93\u51fa\u5230\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u6807\u9898\u6709\u70b9\u7ed5\u53e3\uff0c\u6211\u4eec\u4ee5\u5b9e\u9645\u4f8b\u5b50\u6765\u8bb2\u89e3\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u7684\u4e00\u4e2a\u6587\u4ef6\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ cat \/tmp\/test.txt\r\na char\r\n1 int\r\n2 int\r\nb char\r\nabc string\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6211\u4eec\u7684\u76ee\u6807\u662f\u5c06\u8be5\u6587\u672c\u4e2d\u7684\u884c\u6309\u7b2c\u4e8c\u5217\u7684\u503c\u5f52\u7c7b\uff0c\u5e76\u4e14\u8f93\u51fa\u5230\u76f8\u5e94\u7684\u6587\u4ef6\u4e2d\uff0c\u6587\u4ef6\u540d\u4e3a\u7b2c\u4e8c\u5217\u7684\u540d\u79f0\u3002\u4f8b\u5982\u7b2c2\u884c\u3001\u7b2c3\u884c\u4f1a\u8f93\u51fa\u5230int.txt\u6587\u4ef6\u4e2d\uff0c\u800c\u7b2c1\u884c\u3001\u7b2c4\u884c\u5219\u8f93\u51fa\u5230char.txt\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u6211\u6ca1\u6709\u627e\u5230\u5176\u5b83\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u53ea\u627e\u5230\u4e00\u79cd\u7528awk\u6765\u5904\u7406\u7684\u65b9\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Wed Nov 07 07:31 PM] &#x5B;kodango@devops] ~\/workspace\r\n$ awk '{print $1 &gt; $2 &quot;.txt&quot;}' \/tmp\/test.txt\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6211\u4eec\u6765\u68c0\u67e5\u7ed3\u679c\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Wed Nov 07 07:34 PM] &#x5B;kodango@devops] ~\/workspace\/output\r\n$ grep -nH . *\r\nchar.txt:1:a\r\nchar.txt:2:b\r\nint.txt:1:1\r\nint.txt:2:2\r\nstring.txt:1:abc\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">29\uff09 \u7528exec\u547d\u4ee4\u6765\u5b8c\u6210\u91cd\u5b9a\u5411<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5f00\u59cb\uff0c\u73b0\u5728\u9700\u8981\u4e00\u4e2a\u811a\u672c\uff0c\u5b83\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u540d\u4f5c\u4e3a\u53c2\u6570\uff0c\u7136\u540e\u6309\u884c\u8bfb\u53d6\u8be5\u6587\u4ef6\u7684\u5185\u5bb9\u5e76\u6253\u5370\u5230\u6807\u51c6\u8f93\u51fa\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u6587\u4ef6\u540d\uff0c\u5219\u9ed8\u8ba4\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u3002\u9996\u5148\u6309\u4e0a\u9762\u7684\u529f\u80fd\u9700\u6c42\u5199\u51fa\u4e00\u4e2a\u53ef\u4ee5\u5b8c\u6210\u529f\u80fd\u7684\u811a\u672c\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 10 12:16 AM] &#x5B;kodango@devops] ~\/workspace\r\n$ cat test.sh\r\n\r\nfilename=$1\r\n\r\nif &#x5B; -z &quot;$filename&quot; ]; then\r\n    while read line; do\r\n        echo $line\r\n    done\r\nelse\r\n    while read line; do\r\n        echo $line\r\n    done &lt; $filename\r\nfi\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5982\u679c\u6362exec\u6765\u5b9e\u73b0\u91cd\u5b9a\u5411\uff0c\u53ef\u4ee5\u628a\u811a\u672c\u5199\u5f97\u66f4\u4f18\u96c5\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ cat test1.sh\r\n\r\nfilename=$1\r\n\r\nif &#x5B; -n &quot;$filename&quot; ]; then\r\n    exec 0&lt; $filename\r\nfi\r\n\r\nwhile read line; do\r\n    echo $line\r\ndone\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8fd9\u91cc\u7684\u5173\u952e\u5728\u7b2c5\u884c\u4ee3\u7801\uff0cexec\u547d\u4ee4\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u547d\u4ee4\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u6253\u5f00\u3001\u5173\u95ed\u6216\u8005\u590d\u5236\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u8fd9\u91cc\u5c31\u662f\u5229\u7528exec\u5c06\u6307\u5b9a\u7684\u6587\u4ef6\u540d\u6253\u5f00\u91cd\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u5165\u3002\u7c7b\u4f3c\u5730\u53ef\u4ee5\u7528exec &gt;$filename\u5c06\u6587\u4ef6\u91cd\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u51fa\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e0a\u505a\u4e00\u4e2a\u8bd5\u9a8c\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 10 12:26 AM] &#x5B;kodango@devops] ~\r\n$ exec 3&gt;&amp;1                   # \u9996\u5148\u5c06fd 3\u91cd\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u51fa\uff0c\u4f5c\u4e3a\u6807\u51c6\u8f93\u51fa\u7684\u4e00\u4e2a\u5907\u4efd\r\n\r\n$ ls \/proc\/629\/fd\/{1,3} -l    # \u73b0\u5728fd 3\u548cfd 1\u6307\u5411\u540c\u4e00\u4e2a\u8bbe\u5907\u6587\u4ef6\r\nlrwx------ 1 kodango kodango 64 Nov 10 00:26 \/proc\/629\/fd\/1 -&gt; \/dev\/pts\/1\r\nlrwx------ 1 kodango kodango 64 Nov 10 00:26 \/proc\/629\/fd\/3 -&gt; \/dev\/pts\/1\r\n\r\n$ exec &gt;stdout               # \u73b0\u5728\u628a\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230stdout\u8fd9\u4e2a\u6587\u4ef6\u4e2d\r\n\r\n$ ls \/proc\/629\/fd\/1 -l        # \u5982\u679c\u4f60\u6b64\u523b\u5728\u540c\u4e00\u4e2a\u7ec8\u7aef\u4e0b\u6267\u884c\u672c\u547d\u4ee4\u662f\u6ca1\u6709\u8fd4\u56de\u7684\r\n\r\n$ ls \/proc\/629\/fd\/1 -l        # \u73b0\u5728\u91cd\u65b0\u6253\u5f00\u4e00\u4e2a\u7ec8\u7aef\u770b\u770b\uff0c\u786e\u5b9e\u5df2\u7ecf\u91cd\u5b9a\u5411\u5230stdout\u8fd9\u4e2a\u6587\u4ef6\r\nl-wx------ 1 kodango kodango 64 Nov 10 00:26 \/proc\/629\/fd\/1 -&gt; \/home\/kodango\/stdout\r\n\r\n$ exec 1&gt;&amp;3                   # \u73b0\u5728\u91cd\u65b0\u628a\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u4e4b\u524d\u5907\u4efd\u7684fd 3\u4e0a\r\n$ ls \/proc\/629\/fd\/{1,3} -l  # \u73b0\u5728\u5c4f\u5e55\u53ef\u4ee5\u770b\u5230\u8f93\u51fa\u4e86\uff0c\u4f46\u662ffd 3\u8fd9\u4e2a\u63cf\u8ff0\u7b26\u8fd8\u6253\u5f00\uff0c\u9700\u8981\u5173\u95ed\r\nlrwx------ 1 kodango kodango 64 Nov 10 00:26 \/proc\/629\/fd\/1 -&gt; \/dev\/pts\/1\r\nlrwx------ 1 kodango kodango 64 Nov 10 00:26 \/proc\/629\/fd\/3 -&gt; \/dev\/pts\/1\r\n\r\n$ exec 3&gt;&amp;-                   # \u5173\u95edfd 3\r\n$ ls \/proc\/629\/fd\/3 -l\r\nls: cannot access \/proc\/629\/fd\/3: No such file or directory\r\n\r\n$ cat stdout                  # \u68c0\u67e5stdout\u6587\u4ef6\uff0c\u786e\u5b9e\u6709\u4e4b\u524d\u88ab\u5403\u6389\u7684\u8f93\u51fa\r\nl-wx------ 1 kodango kodango 64 Nov 10 00:26 \/proc\/629\/fd\/1 -&gt; \/home\/kodango\/stdout\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5173\u4e8eI\/O\u91cd\u5b9a\u5411\u7684\u66f4\u8be6\u7ec6\u7684\u8bf4\u660e\uff0c\u53ef\u4ee5\u770b<a href=\"http:\/\/tldp.org\/LDP\/abs\/html\/io-redirection.html\">I\/O Redirection<\/a>\uff0c\u8fd9\u91cc\u6709\u5f88\u591a\u4f8b\u5b50\u8bb2\u89e3\u4e86\u5404\u79cdI\/O\u91cd\u5b9a\u5411\u7684\u7528\u6cd5\uff0c\u5305\u62ecexec\u6765\u6539\u53d8\u91cd\u5b9a\u5411\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u8fd9\u4e00\u70b9\u5728while read; do xxx; done &lt; file\u5185\u90e8\u4ecd\u9700\u8981\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u5185\u5bb9\u65f6\u975e\u5e38\u6709\u7528\uff0c\u6b64\u65f6\u5fc5\u987b\u8981\u5c06\u5faa\u73af\u5916\u90e8\u7684\u91cd\u5b9a\u5411\u548c\u5185\u90e8\u7684\u5265\u79bb\u5f00\u6765\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">30\uff09 \u5f15\u53f7\u4e4b\u95f4\u7684\u533a\u522b<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">Shell\u4e2d\u6bd4\u8f83\u8ba9\u4eba\u6293\u72c2\u7684\u662f\u5404\u79cd\u5f15\u53f7\u7684\u5904\u7406\uff0c\u5176\u4e2d\uff0c\u53cd\u5f15\u53f7(`cmd`)\u662f\u6700\u5bb9\u6613\u638c\u63e1\u7684\uff0c\u5b83\u5176\u5b9e\u548c$(cmd)\u662f\u5dee\u4e0d\u591a\u7684\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5f15\u53f7\u7684\u4f5c\u7528\u6709\u51e0\u70b9\uff0c\u4e00\u4e2a\u662f\u4e3a\u4e86\u5c06\u591a\u4e2a\u56e0\u4e3a\u7a7a\u683c\u6216\u8005\u56de\u8f66\u7b49\u5206\u9694\u7b26\u9694\u5f00\u7684\u5b57\u7b26\u4e32\u5408\u5728\u4e00\u8d77\uff0c\u907f\u514d\u88ab\u547d\u4ee4\u884c\u89e3\u6790\u5206\u5f00\uff0c\u4f8b\u5982&#8221;one two three&#8221;\u5c31\u662f\u4e00\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u662f\u50cfone two three\u4f1a\u88ab\u89e3\u6790\u6210\u4e09\u4e2a\u5355\u72ec\u7684\u5b57\u7b26\u4e32\uff1b\u53e6\u5916\u4e00\u65b9\u9762\uff0c\u5f15\u53f7\u53ef\u4ee5\u8ba9\u4e00\u4e9b\u7279\u6b8a\u7b26\u53f7\u4fdd\u6301\u539f\u4e49\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5176\u4e2d\uff0c\u5355\u5f15\u53f7\u7684\u5904\u7406\u662f\u6bd4\u8f83\u7b80\u5355\u7684\uff0c\u88ab\u5355\u5f15\u53f7\u5305\u62ec\u7684\u6240\u6709\u5b57\u7b26\u90fd\u4fdd\u7559\u539f\u6709\u7684\u610f\u601d\uff0c\u4f8b\u5982&#8217;$a&#8217;\u4e0d\u4f1a\u88ab\u5c55\u5f00, &#8216;`cmd`&#8217;\u4e5f\u4e0d\u4f1a\u6267\u884c\u547d\u4ee4\uff1b\u800c\u53cc\u5f15\u53f7\uff0c\u5219\u76f8\u5bf9\u6bd4\u8f83\u677e\uff0c\u5728\u53cc\u5f15\u53f7\u4e2d\uff0c\u4ee5\u4e0b\u51e0\u4e2a\u5b57\u7b26$, `, \\\u4f9d\u7136\u6709\u5176\u7279\u6b8a\u7684\u542b\u4e49\uff0c\u6bd4\u5982$\u53ef\u4ee5\u7528\u4e8e\u53d8\u91cf\u5c55\u5f00, \u53cd\u5f15\u53f7`\u53ef\u4ee5\u6267\u884c\u547d\u4ee4\uff0c\u53cd\u659c\u6760\\\u53ef\u4ee5\u7528\u4e8e\u8f6c\u4e49\u3002\u4f46\u662f\uff0c\u5728\u53cc\u5f15\u53f7\u5305\u56f4\u7684\u5b57\u7b26\u4e32\u91cc\uff0c\u53cd\u659c\u6760\u7684\u8f6c\u4e49\u4e5f\u662f\u6709\u9650\u7684\uff0c\u5b83\u53ea\u80fd\u8f6c\u4e49$, `, &#8220;, \\\u6216\u8005newline\uff08\u56de\u8f66\uff09\u8fd9\u51e0\u4e2a\u5b57\u7b26\uff0c\u540e\u9762\u5982\u679c\u8ddf\u7740\u7684\u4e0d\u662f\u8fd9\u51e0\u4e2a\u5b57\u7b26\uff0c\u53ea\u4e0d\u4f1a\u88ab\u9ed1\u5e95\uff0c\u53cd\u659c\u6760\u4f1a\u88ab\u4fdd\u7559\uff0c\u4f8b\u5982\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ echo &quot;\\$,\\&quot;,\\`,\\',\\t&quot;\r\n$,&quot;,`,\\',\\t\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u53cc\u5f15\u53f7\u5185\u53ef\u4ee5\u76f4\u63a5\u5305\u542b\u5355\u5f15\u53f7\uff0c\u800c\u4e14\u5355\u5f15\u53f7\u4e5f\u6ca1\u6709\u5982\u4e0a\u636e\u8bf4\u7684\u7279\u6b8a\u542b\u4e49\uff0c\u6240\u4ee5\u50cf&#8221;var=&#8217;$var'&#8221;\u4e2d$var\u8fd8\u662f\u4f1a\u88ab\u5c55\u5f00\u7684\uff0c\u800c\u4e0d\u8981\u4ee5\u4e3a\u7b80\u5355\u5730\u8ba4\u4e3a\u5728\u5355\u5f15\u53f7\u5185\u90e8\u5c31\u4e0d\u4f1a\u5c55\u5f00\u4e86\u3002\u5982\u679c\u53cc\u5f15\u53f7\u5185\u90e8\u5305\u542b\u611f\u53f9\u53f7\uff01\u5c31\u6bd4\u8f83\u5934\u75db\u4e86\uff0c\u611f\u53f9\u53f7\u662f\u7528\u4e8e\u547d\u4ee4\u884c\u5386\u53f2\u5c55\u5f00\uff0c\u4f8b\u5982!!\u5c55\u5f00\u4e3a\u4e0a\u4e00\u6b21\u6267\u884c\u7684\u547d\u4ee4\u3002\u4f60\u53ef\u4ee5\u8bd5\u8bd5\u53cc\u5f15\u53f7\u4e2d\u5305\u542b!\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 10 07:39 PM] &#x5B;kodango@devops] ~\r\n$ echo &quot;!&quot;\r\n-bash: !: event not found\r\n$ echo &quot;\\!&quot;\r\n\\!\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u53ef\u89c1\uff0c\u5373\u4f7f\u4f60\u7528\u53cd\u659c\u6760\u4e5f\u6ca1\u529e\u6cd5\u8f6c\u4e49\uff0c\u9664\u975e\u4f60\u628a\u5386\u53f2\u5c55\u5f00\u529f\u80fd\u5173\u95ed\uff08\u5728\u811a\u672c\u91cc\u9762\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0c\u9ed8\u8ba4\u662f\u5173\u95ed\u7684\uff09\u3002<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 10 07:50 PM] &#x5B;kodango@devops] ~\r\n$ set +o histexpand\r\n\r\n&#x5B;Sat Nov 10 07:50 PM] &#x5B;kodango@devops] ~\r\n$ echo &quot;!&quot;\r\n!\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5f53\u7136\uff0c\u611f\u53f9\u53f7\u53ef\u4ee5\u7528\u5728\u5355\u5f15\u53f7\u91cc\u9762\u3002<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Sat Nov 10 07:50 PM] &#x5B;kodango@devops] ~\r\n$ set -o histexpand\r\n\r\n&#x5B;Sat Nov 10 07:51 PM] &#x5B;kodango@devops] ~\r\n$ echo '!'\r\n!\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5230\u6b64\u4e3a\u6b62\uff0c\u5176\u5b9e\u53cc\u5f15\u53f7\u548c\u5355\u5f15\u53f7\u7684\u533a\u522b\u5df2\u7ecf\u8bf4\u5f97\u5dee\u4e0d\u591a\u4e86\u3002\u4e0d\u8fc7\u8fd8\u53ef\u4ee5\u518d\u8bf4\u51e0\u4e2a\u7279\u6b8a\u7684\u7528\u6cd5\uff0c\u524d\u9762\u8bf4\u8fc7\u53ef\u4ee5\u5728\u53cc\u5f15\u53f7\u5185\u90e8\u4f7f\u7528\u5355\u5f15\u53f7\uff0c\u4f60\u6709\u60f3\u8fc7\u5728\u5355\u5f15\u53f7\u91cc\u9762\u4f7f\u7528\u5355\u5f15\u53f7\u5417\uff1f<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ echo '\\''\r\n&gt;\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u662f\u4e0d\u662f\u53d1\u73b0\u4e0d\u80fd\u7528\uff0c\u56e0\u4e3a\u5355\u5f15\u53f7\u4e2d\u53cd\u659c\u6760\u662f\u6ca1\u6709\u8f6c\u4e49\u7684\u6548\u679c\u7684\uff0c\u4efb\u4f55\u5b57\u7b26\u90fd\u6ca1\u6709\u7279\u6b8a\u7684\u542b\u4e49\u3002\u90a3\u5c31\u6ca1\u6709\u529e\u6cd5\u4e86\u5417\uff1f\u65b9\u6cd5\u603b\u662f\u6709\u7684\uff0c\u53ef\u4ee5\u5728\u7b2c\u4e00\u4e2a\u5355\u5f15\u53f7\u524d\u9762\u52a0\u4e2a$\u7b26\u53f7\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ echo $'\\''\r\n'\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8fd9\u53c8\u662f\u53e6\u5916\u4e00\u79cd\u795e\u5947\u7684\u7528\u6cd5\u4e86\uff0c\u6211\u653e\u5230\u4e0b\u4e00\u70b9\u8bb2\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5173\u4e8e\u8fd9\u4e00\u70b9\u7684\u5185\u5bb9\uff0c\u5177\u4f53\u53ef\u4ee5\u770b\u4ee5\u4e0b\u4e24\u4efd\u8d44\u6599\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\">a. http:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/Quoting.html#Quoting<\/span><br \/>\n<span style=\"font-size: small;\"> b. http:\/\/tldp.org\/LDP\/abs\/html\/quoting.html<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">31\uff09 \u7279\u6b8a\u7528\u6cd5$&#8217;string&#8217;<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u524d\u9762\u4e00\u70b9\u4e2d\u5df2\u7ecf\u4ecb\u7ecd\u4e86 $&#8217;string&#8217;\u8fd9\u79cd\u7528\u6cd5\uff0c\u6bd4\u5982 $&#8217;\\&#8221;\uff0c\u4e4b\u6240\u4ee5\u53ef\u4ee5\u8fd9\u6837\u7528\uff0c\u901a\u4fd7\u5730\u8bb2\uff0c\u5c31\u662f\u5728\u8fd9\u79cd\u8bed\u6cd5\u91cc\u4e00\u4e9b\u8f6c\u4e49\u5b57\u7b26\u4e32\u662f\u88ab\u8ba4\u53ef\u7684\uff0c\u4e8b\u5b9e\u4e0a\u6709\u6548\u5730\u7684\u8f6c\u4e49\u5e95\u5b57\u7b26\u4e32\u5217\u8868\u53ef\u4ee5\u770b<a href=\"http:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/ANSI_002dC-Quoting.html#ANSI_002dC-Quoting\">\u8fd9\u91cc<\/a>\uff0c\u4f8b\u5982\\b\uff0c\\&#8217;,\\n,\\f,\\nnn,\\xhh\u7b49\u7b49\uff0c\u662f\u4e0d\u662f\u5f88\u719f\u6089\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">$&#8217;string&#8217;\u7684\u8fd9\u4e2a\u7279\u6027\uff0c\u5176\u5b9e\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f88\u6709\u7528\u7684\u6280\u5de7\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ echo $'\\x41'\r\nA\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4ed6\u53ef\u4ee5\u5c06ASCII\u5bf9\u5e94\u7684\u5b57\u7b26\u8d4b\u503c\u7ed9\u67d0\u4e2a\u53d8\u91cf\u6216\u8005\u8f93\u51fa\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">32\uff09 \u7528\u53cc\u5f15\u53f7\u6bd4\u4e0d\u7528\u66f4\u52a0\u5b89\u5168<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u53cc\u5f15\u53f7\u9664\u4e86\u524d\u9762\u7b2c10\u70b9\u8bb2\u5230\u7684\u53bb\u9664\u7279\u6b8a\u6db5\u4e49\u7684\u4f5c\u7528\u5916\uff0c\u8fd8\u53ef\u4ee5\u907f\u514d\u5b57\u7b26\u4e32\u88ab\u5206\u9694\u89e3\u6790\uff0c\u4f8b\u5982\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ echo `ls -l`\r\ntotal 4.0K -rw-r--r-- 1 kodango kodango 4 Nov 10 20:09 1 -rw-r--r-- 1 kodango kodango 0 Nov 10 20:09 2\r\n$ echo &quot;`ls -l`&quot;\r\ntotal 4.0K\r\n-rw-r--r-- 1 kodango kodango 4 Nov 10 20:09 1\r\n-rw-r--r-- 1 kodango kodango 0 Nov 10 20:09 2\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u524d\u8005\u6ca1\u6709\u52a0\u53cc\u5f15\u53f7\uff0cls -l\u8f93\u51fa\u884c\u4e4b\u95f4\u7684\u56de\u8f66\u5c31\u88ab\u5403\u6389\u4e86\u3002\u539f\u56e0\u662f\uff0c\u5f53ls -l\u8fd4\u56de\u7684\u7ed3\u679c\u4f20\u9012\u7ed9echo\u4e4b\u524d\uff0c\u4f1a\u5148\u88abshell\u8fdb\u884c\u53c2\u6570\u89e3\u6790\uff0c\u800cshell\u662f\u7528IFS\u5b9a\u4e49\u7684\u5206\u9694\u7b26\u6765\u5206\u9694\u5b57\u7b26\u4e32\u7684\uff0c\u4e00\u822c\u5305\u62ec\\n\uff0c\u6240\u4ee5\u5b83\u628a\u89e3\u6790\u540e\u7684\u7ed3\u679c\u518d\u4f20\u9012\u7ed9echo\uff0c\u5c31\u6210\u4e3aecho &#8220;line 1&#8230;.&#8221; &#8220;line 2&#8230;&#8221;\u8fd9\u79cd\u5f62\u5f0f\u4e86\uff0c\u7ed3\u679c\u5c31\u50cf\u4e0a\u9762\u4e00\u6837\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u800c\u7528\u53cc\u5f15\u53f7\u5305\u62ec\u8d77\u6765\u53ef\u4ee5\u907f\u5f00\u5b57\u7b26\u4e32\u88ab\u62c6\u5f00\u89e3\u6790\uff0c\u56e0\u4e3ashell\u8ba4\u4e3a\u5b83\u662f\u4e00\u4e2a\u5355\u72ec\u7684\u5b57\u7b26\u4e32\u3002\u6240\u4ee5\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u591a\u7528\u5f15\u53f7\u5305\u62ec\u53d8\u91cf\u662f\u597d\u7684\uff0c&#8221;$var&#8221;\u6bd4$var\u66f4\u5b89\u5168\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">33\uff09 \u663e\u793a\u4e00\u4e2a\u6587\u4ef6\u5e76\u4e14\u5728\u6bcf\u884c\u5f00\u5934\u6dfb\u52a0\u884c\u53f7<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u6709\u4e24\u79cd\u505a\u6cd5\uff0c\u7b2c\u4e00\u79cd\u501f\u52a9cat\u548cnl\u547d\u4ee4\u6765\u5b8c\u6210\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ cat test.txt | nl\r\n     1\tline 1\r\n     2\tline 2\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u53e6\u5916\u4e00\u79cd\u505a\u6cd5\u662f\u7528sed\u547d\u4ee4:<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ sed '=' test.txt | sed 'N;s\/\\n\/\\t\/'\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">34\uff09 \u547d\u4ee4\u884c\u952e\u6620\u5c04\uff0c\u7f16\u8f91\u6a21\u5f0f<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u547d\u4ee4\u884c\u4e0b\u9ed8\u8ba4\u662femacs\u7684keymap\uff0c\u5bf9\u4e8e\u4e0d\u4f1aemacs\u7684\u4eba\u6765\u8bf4\u771f\u662f\u707e\u96be\uff0c\u5b8c\u5168\u4e0d\u77e5\u9053\u5404\u79cdctrl+x\u952e\u662f\u505a\u4ec0\u4e48\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u5207\u6362\u5230vi\u6a21\u5f0f\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nset -o vi\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0c\u5c31\u53ef\u4ee5\u7528\u719f\u6089\u7684vi\u547d\u4ee4\u4e86\uff0c\u9ed8\u8ba4\u8f93\u5165\u547d\u4ee4\u7684\u662f\u5728insert\u6a21\u5f0f\uff0c\u6309ESC\u952e\u53ef\u4ee5\u5207\u6362\u5230\u547d\u4ee4\u6a21\u5f0f\uff0c\u8fd9\u70b9\u548cvim\u662f\u4e00\u6837\u7684\uff0c\u719f\u6089vim\u7684\u4eba\u5f88\u5feb\u5c31\u53ef\u4ee5\u4e0a\u624b\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e4b\u524d\u4ecb\u7ecd\u8fc7\u547d\u4ee4\u884c\u4e0b\u4f7f\u7528ctrl+x ctrl+e\u6765\u7f16\u8f91\u5f53\u524d\u547d\u4ee4\uff0c\u800c\u5728vi\u6a21\u5f0f\u4e0b\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u6a21\u5f0f\u4e0b\u76f4\u63a5\u952e\u5165v\u3002\u8fd8\u6709\uff0c\u5982\u679c\u4e0d\u60f3\u6267\u884c\u5f53\u524d\u8f93\u5165\u7684\u547d\u4ee4\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u6a21\u5f0f\u4e0b\u6309#\u53f7\u952e\uff0c\u5b83\u4f1a\u5728\u5f53\u524d\u547d\u4ee4\u5f53\u4f5c\u6ce8\u91ca\u800c\u4e0d\u6267\u884c\uff08\u5728\u547d\u4ee4\u5f00\u5934\u6dfb\u52a0#\u53f7\uff09\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u66f4\u591avi\u6a21\u5f0f\u7684\u4ecb\u7ecd\u53ef\u4ee5\u53c2\u89c1<a href=\"http:\/\/www.catonmat.net\/blog\/bash-vi-editing-mode-cheat-sheet\/\">Working Productively in Bash&#8217;s Vi Command Line Editing Mode<\/a>\uff0c\u4f5c\u8005\u8fd8\u7ed9\u4e86\u4e00\u4efd<a href=\"http:\/\/www.catonmat.net\/download\/bash-vi-editing-mode-cheat-sheet.pdf\">Vi Editing Mode Cheat Sheet<\/a>\u7559\u4f5c\u53c2\u8003\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5982\u679c\u4f60\u60f3\u5c06vi\u6a21\u5f0f\u4f5c\u4e3a\u9ed8\u8ba4\u7684\u7f16\u8f91\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5c06set -o vi\u5199\u5165\u5230~\/.bashrc\u6587\u4ef6\u4e2d\u3002\u5f53\u7136\uff0c\u5728\u8fd0\u7ef4\u7684\u7ebf\u4e0a\u751f\u4ea7\u73af\u5883\u8fd9\u6837\u505a\u662f\u4e0d\u5408\u9002\u7684\uff0c\u4f60\u53ea\u80fd\u624b\u52a8\u8f93\u5165\u5207\u6362\u4e86\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u4f60\u9009\u62e9\u7684ssh\u7ba1\u7406\u5ba2\u6237\u7aef\u6bd4\u8f83\u9ad8\u7ea7\u7684\u8bdd\uff0c\u5e94\u8be5\u53ef\u4ee5\u907f\u514d\u6bcf\u6b21\u624b\u52a8\u8f93\u51fa\u3002\u6bd4\u5982\u6211\u7528\u7684\u662fxshell\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<a href=\"http:\/\/www.netsarang.com\/products\/xsh_key_features.html#L11\">Login script<\/a>\u5728\u6bcf\u6b21\u767b\u5f55\u7684\u65f6\u5019\u81ea\u52a8\u6267\u884c\u547d\u4ee4\uff0c\u6216\u8005\u5c06\u547d\u4ee4\u6dfb\u52a0\u5230<a href=\"http:\/\/www.netsarang.com\/tutorial\/xshell\/1021\/Managing_the_quick_command_sets\">quick command set<\/a>\uff0c\u7136\u540e\u8c03\u51faquick command set toolbar\uff0c\u624b\u52a8\u70b9\u51fb\u6309\u94ae\u5207\u6362\u3002\u8fd9\u4e24\u79cd\u65b9\u6cd5\u7ed3\u5408\u8d77\u6765\u5c31\u51e0\u4e4e\u540c\u5199\u5165\u5230~\/.bashrc\u4e00\u6837\u7684\u65b9\u4fbf\u4e86\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">35\uff09 \u5206\u522b\u8f93\u51fa\u4e24\u4e2a\u6587\u4ef6\u76f8\u540c\u7684\u884c\u548c\u4e0d\u540c\u7684\u884c<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u4e24\u4e2a\u6587\u4ef6\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ echo test{,2}.txt;paste test{,2}.txt\r\ntest.txt test2.txt\r\nline 1 \tline 11\r\nline 2\tline 2\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5982\u679c\u8981\u8f93\u51fa\u4e24\u4e2a\u6587\u4ef6\u4e4b\u95f4\u76f8\u540c\u7684\u884c\uff0c\u53ea\u6709test.txt\u62e5\u6709\u7684\u884c\u4ee5\u53ca\u53ea\u6709test2.txt\u62e5\u6709\u7684\u884c\uff0c\u600e\u4e48\u505a\uff1f\u9996\u5148\u53ef\u4ee5\u4f7f\u7528grep -f\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ grep -f test{,2}.txt\r\nline 2\r\n$ grep -vf test{,2}.txt\r\nline 11\r\n$ grep -vf test{2,}.txt\r\nline 1\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8fd8\u6709\u4e00\u79cd\u9009\u62e9\u662fcomm\u547d\u4ee4\uff0c\u8fd9\u4e2a\u547d\u4ee4\u5c31\u662f\u4e13\u95e8\u7528\u4e8e\u6bd4\u8f83\u6587\u4ef6\u7684\uff1a comm &#8211; compare two sorted files line by line\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u4f7f\u7528\u65b9\u6cd5\u4e5f\u5f88\u7b80\u5355\uff0ccomm\u6bd4\u8f83\u4e24\u4e2a\u6392\u5e8f\u597d\u7684\u6587\u4ef6\u8fd4\u56de\u7684\u7ed3\u679c\u6709\u4e09\u5217\uff0c\u7b2c\u4e00\u5217\u662f\u53ea\u6709\u5728\u6587\u4ef6A\u4e2d\u6709\u7684\u884c\uff0c\u7b2c\u4e8c\u5217\u662f\u53ea\u6709\u5728\u6587\u4ef6B\u4e2d\u6709\u7684\u884c\uff0c\u7b2c\u4e09\u5217\u5219\u662f\u4e24\u4e2a\u6587\u4ef6\u5171\u6709\u7684\u884c\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ comm test.txt test2.txt\r\nline 1\r\n\tline 11\r\n\t\tline 2\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8981\u5f97\u5230\u6700\u521d\u8981\u6c42\u7684\u7ed3\u679c\uff0c\u5219\u53ea\u9700\u8981\u53d6\u76f8\u5e94\u7684\u5217\u5c31\u53ef\u4ee5\u4e86\u3002comm\u547d\u4ee4\u975e\u5e38\u4eba\u6027\u5316\u5730\u8003\u8651\u5230\u8fd9\u4e2a\u9700\u6c42\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ comm test.txt test2.txt -1 -2\r\nline 2\r\n$ comm test.txt test2.txt -2 -3\r\nline 1\r\n$ comm test.txt test2.txt -1 -3\r\nline 11\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5176\u4e2d\uff0c=1, -2\u4e0e-3\u8fd9\u4e2a\u53c2\u6570\u5206\u522b\u8868\u793a\u4e0d\u8f93\u51fa\u7b2c1\u30012\u6216\u80053\u5217\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">36\uff09 \u83b7\u53d6\u88absource\u7684\u811a\u672c\u7684\u540d\u79f0<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u4e00\u822c\u7684\u60c5\u51b5\u4e0b\uff0c\u811a\u672c\u7684\u540d\u79f0\u53ef\u4ee5\u901a\u8fc7$0\u83b7\u53d6\uff0c\u4f46\u662f\u8fd9\u5728\u88absource\u5bfc\u5165\u7684\u811a\u672c\u4e2d\u5374\u4e0d\u6210\u7acb\u3002\u5047\u8bbeA\u811a\u672c\u4e2dsource\u4e86B\u811a\u672c\uff0c\u90a3\u4e48\u5b83\u662f\u628aB\u7684\u4ee3\u7801\u5bfc\u5165\u5230A\u7684\u73af\u5883\u4e2d\u76f4\u63a5\u6267\u884c\u7684\uff0c\u56e0\u6b64A\u548cB\u7684\u4ee3\u7801\u5176\u5b9e\u662f\u5728\u540c\u4e00\u4e2a\u6267\u884c\u73af\u5883\u4e0b\u5206\u4e0d\u5f00\u7684\uff0cB\u7684\u4ee3\u7801\u4e2d\u8bbf\u95ee\u5230\u7684$0\uff0c\u751a\u81f3$1, $2\u7b49\u4f4d\u7f6e\u53c2\u6570\u90fd\u662f\u4e0eA\u811a\u672c\u662f\u4e00\u81f4\u7684\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u56e0\u6b64$0\u5e76\u975e\u662f\u88ab\u5bfc\u5165\u7684\u811a\u672c\u7684\u540d\u79f0\uff0c\u5b9e\u9645\u4e0a\uff0cBash\u5c06\u88absource\u7684\u811a\u672c\u540d\u79f0\u4fdd\u5b58\u5728\u4e00\u4e2a\u53ebBASH_SOURCE\u7684\u6570\u7ec4\u4e2d\uff0c\u8be5\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u6b63\u662f\u5f53\u524d\u88absource\u7684\u811a\u672c\u7684\u540d\u79f0\u3002\u8be5\u53d8\u91cf\u4e0e\u6211\u5728<a href=\"http:\/\/kodango.me\/get-function-name-in-bash\">bash\u83b7\u53d6\u5f53\u524d\u51fd\u6570\u540d<\/a>\u4e2d\u4ecb\u7ecd\u7684FUNCNAME\u662f\u7c7b\u4f3c\u7684\uff0c\u5f53\u4e00\u4e2a\u811a\u672c\u88absource\u65f6\uff0c\u5b83\u7684\u540d\u79f0\u5c31\u88ab\u538b\u5165\u5230\u8fd9\u4e2a\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u4e0a\uff0c\u4e3e\u4e2a\u5b9e\u9645\u7684\u4f8b\u5b50\uff0c\u5047\u8bbe\u6709\u4e09\u4e2a\u811a\u672ca.sh,b.sh,c.sh\uff0c\u5b83\u4eec\u7684\u5185\u5bb9\u5982\u4e0b\u6240\u793a\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ cat a.sh\r\n. .\/b.sh\r\necho &quot;\\$0=$0&quot;\r\necho &quot;\\${BASH_SOURCE&#x5B;0]}=${BASH_SOURCE&#x5B;0]}&quot;\r\necho &quot;\\$BASH_SOURCE=(${BASH_SOURCE&#x5B;@]})&quot;\r\n\r\n$ cat b.sh\r\n. .\/c.sh\r\n. .\/c.sh\r\necho &quot;\\$0=$0&quot;\r\necho &quot;\\${BASH_SOURCE&#x5B;0]}=${BASH_SOURCE&#x5B;0]}&quot;\r\necho &quot;\\$BASH_SOURCE=(${BASH_SOURCE&#x5B;@]})&quot;\r\n\r\n$ cat c.sh\r\n$ cat c.sh\r\necho &quot;\\$0=$0&quot;\r\necho &quot;\\${BASH_SOURCE&#x5B;0]}=${BASH_SOURCE&#x5B;0]}&quot;\r\necho &quot;\\$BASH_SOURCE=(${BASH_SOURCE&#x5B;@]})&quot;\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u73b0\u5728\u6267\u884ca.sh\u8fd9\u4e2a\u811a\u672c\uff0c\u5b9e\u9645\u7684\u8f93\u51fa\u662f\uff08\u4e3a\u4e86\u65b9\u4fbf\u7406\u89e3\uff0c\u6211\u5728\u5b9e\u9645\u7684\u8f93\u51fa\u4e2d\u52a0\u4e86\u4e00\u4e9b\u6ce8\u91ca\u548c\u7a7a\u884c\uff09\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ bash a.sh\r\n# c.sh\u7684\u8f93\u51fa\r\n$0=a.sh\r\n${BASH_SOURCE&#x5B;0]}=.\/c.sh\r\n$BASH_SOURCE=(.\/c.sh .\/b.sh a.sh)\r\n\r\n# b.sh\u7684\u8f93\u51fa\r\n$0=a.sh\r\n${BASH_SOURCE&#x5B;0]}=.\/b.sh\r\n$BASH_SOURCE=(.\/b.sh a.sh)\r\n\r\n# a.sh\u7684\u8f93\u51fa\r\n$0=a.sh\r\n${BASH_SOURCE&#x5B;0]}=a.sh\r\n$BASH_SOURCE=(a.sh)\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5229\u7528BASH_SOURCE\u7684\u503c\uff0c\u5728\u811a\u672c\u4e2d\u5224\u65ad\u662f\u88ab\u76f4\u63a5\u6267\u884c\u8fd8\u662f\u88ab\u5bfc\u5165\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nif &#x5B; -n &quot;$BASH_SOURCE&quot; -a &quot;$BASH_SOURCE&quot; != &quot;$0&quot; ]\r\nthen\r\n    echo &quot;be sourced by other scripts&quot;\r\nelse\r\n    echo &quot;be run in shell&quot;\r\nfi\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">37\uff09 ${}\u53c2\u6570\u5c55\u5f00<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u6211\u4eec\u77e5\u9053${parameter}\u662f\u5c55\u5f00\u53d8\u91cfparameter\u8fd9\u4e2a\u503c\uff0c\u5728\u4e0a\u4e00\u7bc7\u7b80\u6d01\u7684bash\u7f16\u7a0b\u6280\u5de7\u4e2d\u4e5f\u66fe\u7ecf\u4ecb\u7ecd\u8fc7${parameter:-word}\u8fd9\u79cd\u7528\u6cd5\uff0c\u7528\u4e8e\u7ed9\u53d8\u91cf\u8d4b\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e8b\u5b9e\u4e0a\u9664\u6b64\u4e4b\u5916\uff0c\u53c2\u6570\u5c55\u5f00\u8fd8\u6709\u8bb8\u591a\u5f62\u5f0f\uff0c\u5728\u6b64\u4e4b\u524d\uff0c\u9996\u5148\u8981\u8bf4\u660e\u4e00\u4e0b\u53d8\u91cf\u7684\u51e0\u79cd\u503c\u7684\u5f62\u5f0f\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\">1. unset\uff1a \u53d8\u91cf\u672a\u8bbe\u7f6e\uff0c\u5373\u53d8\u91cf\u4ece\u672a\u58f0\u660e\uff0c\u6216\u8005\u88abunset\u547d\u4ee4\u91cd\u7f6e\uff1b<\/span><br \/>\n<span style=\"font-size: small;\"> 2. null: \u53d8\u91cf\u58f0\u660e\u4f46\u672a\u88ab\u8d4b\u503c\uff08var=\uff09\u6216\u8005\u88ab\u8d4b\u503c\u6210\u7a7a\uff08var=&#8221;&#8221;\uff09\uff1b<\/span><br \/>\n<span style=\"font-size: small;\"> 3. not null\uff1a \u53d8\u91cf\u88ab\u8d4b\u503c\uff1b<\/span><\/p>\n<p><span style=\"font-size: small;\">unset\u548cnull\u5728\u53c2\u6570\u5c55\u5f00\u7684\u65f6\u5019\u8fd8\u662f\u6709\u5f88\u5927\u7684\u533a\u522b\u7684\uff0c\u4ee5\u4e0b\u662f\u53c2\u6570\u5c55\u5f00\u7684\u5404\u79cd\u5f62\u5f0f\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\">1. ${parameter:-word}\uff1a\u5047\u5982parameter\u4e3aunset\u6216\u8005null\uff0c\u5219\u5c55\u5f00\u540e\u8fd4\u56deword\u7684\u503c\uff1b<\/span><br \/>\n<span style=\"font-size: small;\"> 2. ${parameter-word}\uff1a\u5047\u5982parameter\u4e3aunset\u65f6\uff0c\u5219\u5c55\u5f00\u540e\u8fd4\u56deword\u7684\u503c\uff1b<\/span><br \/>\n<span style=\"font-size: small;\"> 3. ${parameter:=word}\uff1a\u5047\u5982parameter\u4e3aunset\u6216\u8005null\uff0c\u5c06word\u8d4b\u503c\u7ed9parameter\uff1b<\/span><br \/>\n<span style=\"font-size: small;\"> 4. ${parameter=word}\uff1a\u5047\u5982parameter\u4e3aunset\uff0c\u5c06word\u8d4b\u503c\u7ed9parameter\uff1b<\/span><br \/>\n<span style=\"font-size: small;\"> 5. ${parameter:?word}\uff1a\u5047\u5982parameter\u4e3aunset\u6216\u8005null\uff0c\u5219\u5c06word\u4f5c\u4e3a\u9519\u8bef\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\uff1b<\/span><br \/>\n<span style=\"font-size: small;\"> 6. ${parameter?word}\uff1a\u5047\u5982parameter\u4e3aunset\uff0c\u5219\u5c06word\u4f5c\u4e3a\u9519\u8bef\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\uff1b<\/span><br \/>\n<span style=\"font-size: small;\"> 7. ${parameter:+word}\uff1a\u5047\u5982parameter\u4e3aunset\u6216\u8005null\uff0c\u5219\u4e0d\u505a\u5c55\u5f00\uff0c\u8fd4\u56de\u4e3a\u7a7a\uff1b\uff08\u521a\u597d\u4e0e:-\u76f8\u53cd\uff09<\/span><br \/>\n<span style=\"font-size: small;\"> 8. ${parameter:word}\uff1a\u5047\u5982parameter\u4e3aunset\uff0c\u5219\u4e0d\u505a\u5c55\u5f00\uff0c\u8fd4\u56de\u4e3a\u7a7a\uff1b\uff08\u521a\u597d\u4e0e-\u76f8\u53cd\uff09<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e0a\u9762\u5176\u5b9e\u51c6\u786e\u5730\u5e94\u8be5\u662f\u5206\u62102\u7ec4\uff0c\u4e00\u7ec4\u5e26:\uff0c\u4e00\u7ec4\u4e0d\u5e26:\uff0c\u4e0d\u5e26:\u7684\u8fd9\u7ec4\u66f4\u52a0\u4e25\u683c\uff0c\u53ea\u68c0\u67e5unset\u8fd9\u79cd\u60c5\u51b5\u3002\u4ee5:+\u4e3a\u4f8b\u5b50, unset\u7684\u60c5\u51b5\u5747\u65e0\u8fd4\u56de\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&lt;span style=&quot;font-size: small;&quot;&gt;$ unset var &amp;&amp; echo ${var:+hello}&lt;\/span&gt;\r\n\r\n&lt;span style=&quot;font-size: small;&quot;&gt;$ unset var &amp;&amp; echo ${var+hello}&lt;\/span&gt;\r\n&lt;span style=&quot;font-size: small;&quot;&gt;<\/pre>\n<p><\/span><br \/>\n<span style=\"font-size: small;\">\u5f53var\u4e3a\u7a7a\u65f6\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ var= &amp;&amp; echo &quot;${var:+hello}&quot;\r\n\r\n$ var= &amp;&amp; echo &quot;${var+hello}&quot;\r\nhello\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5f53var\u4e3a\u975e\u7a7a\u65f6\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ var=1 &amp;&amp; echo &quot;${var:+hello}&quot;\r\nhello\r\n$ var=1 &amp;&amp; echo &quot;${var+hello}&quot;\r\nhello\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5173\u4e8e\u53c2\u6570\u5c55\u5f00\u7684\u5177\u4f53\u5185\u5bb9\u53ef\u4ee5\u53c2\u8003Bash Man\u624b\u518c\u4e2d\u7684Parameter Expansion\u8fd9\u4e00\u8282\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">38\uff09 \u5192\u53f7\u7684\u591a\u79cd\u4f7f\u7528\u573a\u666f<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u5192\u53f7\u662f\u4e00\u4e2a\u6bd4\u8f83\u5947\u602a\u7684\u7b26\u53f7\uff0c\u5b83\u7684\u7528\u9014\u6709\u5f88\u591a\uff0c\u8fd9\u91cc\u4ecb\u7ecd\u51e0\u79cd\u5e38\u7528\u7684\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\">1. \u5185\u7f6e\u547d\u4ee4null command\uff1anop\uff0c\u8868\u793a\u4ec0\u4e48\u90fd\u4e0d\u505a\uff0c\u4e5f\u53ef\u4ee5\u88ab\u5f53\u4f5ctrue\u503c\u4f7f\u7528\uff1b<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ :\r\n$ echo $?    # return 0\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5b83\u4e5f\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u5f53\u4f5ctrue\u503c\uff0c\u4f8b\u5982:<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nwhile :; do   # \u7b49\u4ef7\u4e8e while true; do\r\n    take-some-action\r\ndone\r\n\r\nif condition\r\nthen :\r\nelse\r\n    take-some-action\r\nfi\r\n<\/pre>\n<p><span style=\"font-size: small;\">2. \u5360\u4f4d\u7b26<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5192\u53f7\u53ef\u4ee5\u5728\u5f88\u591a\u573a\u666f\u4e0b\u5145\u5f53\u5360\u4f4d\u7b26\uff0c\u4f8b\u5982\u4e4b\u524d\u4ecb\u7ecd\u7684${parameter=var}\uff0c\u5982\u679c\u76f4\u63a5\u6267\u884c\u4f1a\u62a5\u9519\uff0c\u8868\u793a\u627e\u4e0d\u5230\u547d\u4ee4\uff1b\u8fd9\u65f6\u53ef\u4ee5\u501f\u7528\u5192\u53f7\u6765\u5b8c\u6210\u8d4b\u503c\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n: ${parameter=var}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u540c\u6837\u5730\uff0c\u53ef\u4ee5\u6765\u5224\u65ad\u53d8\u91cf\u662f\u5426\u8d4b\u503c\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n: ${parameter1?} ${parameter2?}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u66f4\u591a\u5176\u5b83\u7528\u6cd5\u53ef\u4ee5\u770bABS\u7684<a href=\"http:\/\/www.tldp.org\/LDP\/abs\/html\/special-chars.html#COLON0REF\">Special Characters<\/a>\u8fd9\u4e00\u8282\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">39\uff09 \u6269\u5c55\u7684\u62ec\u53f7\u5c55\u5f00\u529f\u80fd<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u8fd9\u4e2a\u529f\u80fd\u4e0d\u80fd\u8bf4\u9e21\u808b\uff0c\u4e5f\u53ef\u4ee5\u4e86\u89e3\u4e0b\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ echo {0..3}\r\n0 1 2 3\r\n$ echo {z..a}\r\nz y x w v u t s r q p o n m l k j i h g f e d c b a\r\n$ echo {a..z}\r\na b c d e f g h i j k l m n o p q r s t u v w x y z\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">40\uff09 [[]]\u6bd4[]\u4f5c\u6761\u4ef6\u6d4b\u8bd5\u66f4\u5b89\u5168<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">[[]]\u7684\u529f\u80fd\u6bd4[]\u66f4\u52a0\u591a\uff0c\u4f7f\u7528\u8d77\u6765\u4e5f\u66f4\u52a0\u5b89\u5168\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">1. \u9996\u5148[[]]\u5185\u90e8\u4e0d\u4f1a\u53d1\u751f\u6587\u4ef6\u540d\u5c55\u5f00\u548c\u5355\u8bcd\u5206\u9694\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4f8b\u5982\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ touch hello\\ world\r\n$ &#x5B;&#x5B; -f $file ]] &amp;&amp; echo yes\r\nyes\r\n$ &#x5B; -f $file ] &amp;&amp; echo yes\r\n-bash: &#x5B;: hello: binary operator expected\r\n<\/pre>\n<p><span style=\"font-size: small;\">2. \u8fdb\u5236\u4e4b\u95f4\u81ea\u52a8\u8f6c\u5316<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5f53\u4e00\u4e2a\u5341\u8fdb\u5236\u4e0e\u516b\u8fdb\u5236\u505a\u6bd4\u8f83\u65f6\uff0c\u4f1a\u81ea\u52a8\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u503c\uff0c\u7edf\u4e00\u540e\u505a\u6bd4\u8f83\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ o=017\r\n$ h=0x0f\r\n$ &#x5B;&#x5B; $o -eq $h ]] &amp;&amp; echo yes\r\nyes\r\n$ &#x5B;&#x5B; $o -eq 15 ]] &amp;&amp; echo yes\r\nyes\r\n<\/pre>\n<p><span style=\"font-size: small;\">3. [[]]\u652f\u6301&amp;&amp;\uff0c||\u7b49\u8fd0\u7b97\u7b26<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ a=1;b=3\r\n$ &#x5B;&#x5B; $a &gt; 0 &amp;&amp; $b &lt; 4 ]] &amp;&amp; echo yes\r\nyes\r\n<\/pre>\n<p><strong><span style=\"font-size: small;\">41\uff09 \u83b7\u53d6Bash\u811a\u672c\u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u6211\u4eec\u90fd\u77e5\u9053\u53ef\u4ee5\u7528$0\uff0c$1\u7b49\u6765\u83b7\u53d6\u4f20\u9012\u7ed9\u811a\u672c\u6216\u8005\u51fd\u6570\u7684\u53c2\u6570\uff0c\u4e5f\u53ef\u4ee5\u7528$*\u6216\u8005$@\u83b7\u53d6\u6240\u6709\u7684\u53c2\u6570\uff0c\u4f46\u662f\u5982\u679c\u6211\u53ea\u60f3\u8981\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u5462\uff1f<\/span><\/p>\n<p><span style=\"font-size: small;\">\u9996\u5148\uff0c\u4f60\u53ef\u80fd\u60f3\u5230\u7528\u904d\u5386\u5730\u65b9\u6cd5\uff08\u8fd9\u91cc\u4e3a\u4e86\u65b9\u4fbf\uff0c\u6211\u4eec\u4f7f\u7528set\u547d\u4ee4\u6765\u8bbe\u7f6e\u4f4d\u7f6e\u53c2\u6570\uff09\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ set -- arg1 arg2 arg3\r\n$ for i in $@; do :; done\r\n$ echo $i\r\narg3\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8fd9\u91cc\u7684\u5faa\u73af\u4ec0\u4e48\u4e8b\u60c5\u90fd\u6ca1\u505a\uff0c\u6211\u7528\u5192\u53f7\uff08:\uff09\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\uff1b\u5faa\u73af\u7ed3\u675f\u540e, $i\u5c31\u662f\u4fdd\u5b58\u7740\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u7684\u503c\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e0b\u9762\u662f\u4e24\u79cd\u66f4\u52a0\u7b80\u5355\u7684\u65b9\u6cd5\u7684\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ echo ${@: -1}\r\n$ echo ${!#}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4e0a\u9762\u7684\u7b2c\u4e00\u79cd\u65b9\u6cd5\u4e8b\u5b9e\u4e0a\u5c31\u662fParameter Expansion\u4e2d\u7684${parameter:offset:length}\u8fd9\u79cd\u5f62\u5f0f\uff0c\u53ea\u4e0d\u8fc7offset\u4e3a-1\u8868\u793a\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u5ffd\u7565length\u8868\u660e\u662f\u4eceoffset\u5f00\u59cb\u5f80\u540e\u76f4\u5230\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u5373\u53ea\u53d6\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u8fd9\u91cc\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0c\u5728\u5192\u53f7\u548c\u77ed\u6a2a\u4e4b\u95f4\u7684\u7a7a\u683c\u4e0d\u80fd\u5c11\uff0c\u5426\u5219\u5c31\u53d8\u621015. ${}\u53c2\u6570\u5c55\u5f00\u4e2d\u4ecb\u7ecd\u7684${parameter:-var}\u8fd9\u79cd\u7528\u6cd5\u4e86\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u800c\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u5219\u662findirect referencing\u7684\u4e00\u79cd\u8868\u73b0\uff0c#\u8fd9\u4e2a\u7279\u6b8a\u7684\u53d8\u91cf\u5b58\u653e\u53c2\u6570\u7684\u4e2a\u6570\uff0c!#\u5219\u662f\u5bf9\u6700\u540e\u4e00\u4e2a\u53d8\u91cf\u7684\u5f15\u7528\u3002<\/span><\/p>\n<p><strong><span style=\"font-size: small;\">42\uff09 Bash\u4e2d\u7684\u5f15\u7528(indirect referencing)<\/span><\/strong><\/p>\n<p><span style=\"font-size: small;\">\u6709\u6ca1\u6709\u60f3\u6cd5\u5728Bash\u4e2d\u4e5f\u53ef\u4ee5\u8fbe\u5230C++\u5f15\u7528\u7684\u6548\u679c\uff1f\u4f60\u53ef\u80fd\u4e0d\u77e5\u9053\uff0c\u4f46\u662f\u4f60\u53ef\u80fd\u66fe\u7ecf\u6709\u8fd9\u79cd\u9700\u6c42\uff0c\u6211\u5c31\u6709\u8fc7\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\">\u6709\u65f6\u5019\uff0c\u6211\u60f3\u8981\u4e00\u4e2a\u53d8\u91cf\u5b58\u653e\u53e6\u5916\u4e00\u4e2a\u53d8\u91cf\u7684\u540d\u79f0\uff0c\u7136\u540e\u5728\u540e\u9762\u6211\u60f3\u901a\u8fc7\u8fd9\u4e2a\u53d8\u91cf\u7684\u540d\u79f0\u5f15\u7528\u5b83\u7684\u503c<\/span><br \/>\n<span style=\"font-size: small;\"> \u4f8b\u5b50\u662f\u8fd9\u6837\u7684\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ a=b\r\n$ b=1\r\n$ echo $a\r\nb\r\n$ eval &quot;echo \\$$a&quot;\r\n1\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4f46\u662f\u5229\u7528indirect referencing\u7684\u7528\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u83b7\u53d6b\u7684\u503c:<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ echo ${!a}\r\n1\r\n$ b=2\r\n$ echo ${!a}\r\n2\r\n\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u9644\u51e0\u5219\u5c0f\u6280\u5de7\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\">1\uff09sudo iptables -L -n | vim &#8211;<\/span><br \/>\n<span style=\"font-size: small;\"> 2\uff09grep -v xxx | vim &#8211;<\/span><br \/>\n<span style=\"font-size: small;\"> 3\uff09echo $&#8217;\\&#8221;<\/span><br \/>\n<span style=\"font-size: small;\"> 4\uff09set &#8212; 1 2 3; echo &#8220;$@&#8221;<\/span><br \/>\n<span style=\"font-size: small;\"> 5\uff09\u641c\u7d22stackoverflow\/superuser\u7b49\u7ad9\u70b9<\/span><\/p>\n<p><span style=\"font-size: small; color: #ff0000;\">\u539f\u6587\uff1ahttp:\/\/kodango.me\/simple-bash-programming-skills<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u8fd9\u51e0\u6761\u662f\u6211\u81ea\u5df1\u5728\u5199shell\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u6bd4\u8f83\u559c\u6b22\u7684\u51e0\u79cd\u5199\u6cd5\uff0c\u629b\u7816\u5f15\u7389\u3002 1) \u68c0\u67e5\u547d\u4ee4\u6267\u884c\u662f\u5426\u6210\u529f \u7b2c\u4e00\u79cd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-2292","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=2292"}],"version-history":[{"count":3,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2292\/revisions"}],"predecessor-version":[{"id":2294,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2292\/revisions\/2294"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=2292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=2292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=2292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}