{"id":5336,"date":"2022-08-08T17:50:38","date_gmt":"2022-08-08T09:50:38","guid":{"rendered":"http:\/\/139.9.1.231\/?p=5336"},"modified":"2022-08-08T20:28:37","modified_gmt":"2022-08-08T12:28:37","slug":"bash-jiaoben","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/08\/08\/bash-jiaoben\/","title":{"rendered":"Bash \u811a\u672c\u5165\u95e8"},"content":{"rendered":"\n<p># Bash \u811a\u672c\u5165\u95e8<\/p>\n\n\n\n<p>\u811a\u672c\uff08script\uff09\u5c31\u662f\u5305\u542b\u4e00\u7cfb\u5217\u547d\u4ee4\u7684\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u3002Shell \u8bfb\u53d6\u8fd9\u4e2a\u6587\u4ef6\uff0c\u4f9d\u6b21\u6267\u884c\u91cc\u9762\u7684\u6240\u6709\u547d\u4ee4\uff0c\u5c31\u597d\u50cf\u8fd9\u4e9b\u547d\u4ee4\u76f4\u63a5\u8f93\u5165\u5230\u547d\u4ee4\u884c\u4e00\u6837\u3002\u6240\u6709\u80fd\u591f\u5728\u547d\u4ee4\u884c\u5b8c\u6210\u7684\u4efb\u52a1\uff0c\u90fd\u80fd\u591f\u7528\u811a\u672c\u5b8c\u6210\u3002<\/p>\n\n\n\n<p>\u811a\u672c\u7684\u597d\u5904\u662f\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u6307\u5b9a\u5728\u7279\u5b9a\u573a\u5408\u81ea\u52a8\u8c03\u7528\uff0c\u6bd4\u5982\u7cfb\u7edf\u542f\u52a8\u6216\u5173\u95ed\u65f6\u81ea\u52a8\u6267\u884c\u811a\u672c\u3002<\/p>\n\n\n\n<h2>Shebang \u884c<\/h2>\n\n\n\n<p>\u811a\u672c\u7684\u7b2c\u4e00\u884c\u901a\u5e38\u662f\u6307\u5b9a\u89e3\u91ca\u5668\uff0c\u5373\u8fd9\u4e2a\u811a\u672c\u5fc5\u987b\u901a\u8fc7\u4ec0\u4e48\u89e3\u91ca\u5668\u6267\u884c\u3002\u8fd9\u4e00\u884c\u4ee5<code>#!<\/code>\u5b57\u7b26\u5f00\u5934\uff0c\u8fd9\u4e2a\u5b57\u7b26\u79f0\u4e3a Shebang\uff0c\u6240\u4ee5\u8fd9\u4e00\u884c\u5c31\u53eb\u505a Shebang \u884c\u3002<\/p>\n\n\n\n<p><code>#!<\/code>\u540e\u9762\u5c31\u662f\u811a\u672c\u89e3\u91ca\u5668\u7684\u4f4d\u7f6e\uff0cBash \u811a\u672c\u7684\u89e3\u91ca\u5668\u4e00\u822c\u662f<code>\/bin\/sh<\/code>\u6216<code>\/bin\/bash<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/sh\n# \u6216\u8005\n#!\/bin\/bash\n<\/code><\/pre>\n\n\n\n<p><code>#!<\/code>\u4e0e\u811a\u672c\u89e3\u91ca\u5668\u4e4b\u95f4\u6709\u6ca1\u6709\u7a7a\u683c\uff0c\u90fd\u662f\u53ef\u4ee5\u7684\u3002<\/p>\n\n\n\n<p>\u5982\u679c Bash \u89e3\u91ca\u5668\u4e0d\u653e\u5728\u76ee\u5f55<code>\/bin<\/code>\uff0c\u811a\u672c\u5c31\u65e0\u6cd5\u6267\u884c\u4e86\u3002\u4e3a\u4e86\u4fdd\u9669\uff0c\u53ef\u4ee5\u5199\u6210\u4e0b\u9762\u8fd9\u6837\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env bash\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u547d\u4ee4\u4f7f\u7528<code>env<\/code>\u547d\u4ee4\uff08\u8fd9\u4e2a\u547d\u4ee4\u603b\u662f\u5728<code>\/usr\/bin<\/code>\u76ee\u5f55\uff09\uff0c\u8fd4\u56de Bash \u53ef\u6267\u884c\u6587\u4ef6\u7684\u4f4d\u7f6e\u3002<code>env<\/code>\u547d\u4ee4\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u8bf7\u770b\u540e\u6587\u3002<\/p>\n\n\n\n<p>Shebang \u884c\u4e0d\u662f\u5fc5\u9700\u7684\uff0c\u4f46\u662f\u5efa\u8bae\u52a0\u4e0a\u8fd9\u884c\u3002\u5982\u679c\u7f3a\u5c11\u8be5\u884c\uff0c\u5c31\u9700\u8981\u624b\u52a8\u5c06\u811a\u672c\u4f20\u7ed9\u89e3\u91ca\u5668\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u811a\u672c\u662f<code>script.sh<\/code>\uff0c\u6709 Shebang \u884c\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u6267\u884c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ .\/script.sh\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c<code>script.sh<\/code>\u662f\u811a\u672c\u6587\u4ef6\u540d\u3002\u811a\u672c\u901a\u5e38\u4f7f\u7528<code>.sh<\/code>\u540e\u7f00\u540d\uff0c\u4e0d\u8fc7\u8fd9\u4e0d\u662f\u5fc5\u9700\u7684\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">\u5982\u679c\u6ca1\u6709 Shebang \u884c\uff0c\u5c31\u53ea\u80fd\u624b\u52a8\u5c06\u811a\u672c\u4f20\u7ed9\u89e3\u91ca\u5668\u6765\u6267\u884c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ \/bin\/sh .\/script.sh\n# \u6216\u8005\n$ bash .\/script.sh\n<\/code><\/pre>\n\n\n\n<h2>\u6267\u884c\u6743\u9650\u548c\u8def\u5f84<\/h2>\n\n\n\n<p>\u524d\u9762\u8bf4\u8fc7\uff0c\u53ea\u8981\u6307\u5b9a\u4e86 Shebang \u884c\u7684\u811a\u672c\uff0c\u53ef\u4ee5\u76f4\u63a5\u6267\u884c\u3002\u8fd9\u6709\u4e00\u4e2a\u524d\u63d0\u6761\u4ef6\uff0c\u5c31\u662f\u811a\u672c\u9700\u8981\u6709\u6267\u884c\u6743\u9650\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\uff0c\u8d4b\u4e88\u811a\u672c\u6267\u884c\u6743\u9650\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u7ed9\u6240\u6709\u7528\u6237\u6267\u884c\u6743\u9650\n$ chmod +x script.sh\n\n# \u7ed9\u6240\u6709\u7528\u6237\u8bfb\u6743\u9650\u548c\u6267\u884c\u6743\u9650\n$ chmod +rx script.sh\n# \u6216\u8005\n$ chmod 755 script.sh\n\n# \u53ea\u7ed9\u811a\u672c\u62e5\u6709\u8005\u8bfb\u6743\u9650\u548c\u6267\u884c\u6743\u9650\n$ chmod u+rx script.sh\n<\/code><\/pre>\n\n\n\n<p>\u811a\u672c\u7684\u6743\u9650\u901a\u5e38\u8bbe\u4e3a<code>755<\/code>\uff08\u62e5\u6709\u8005\u6709\u6240\u6709\u6743\u9650\uff0c\u5176\u4ed6\u4eba\u6709\u8bfb\u548c\u6267\u884c\u6743\u9650\uff09\u6216\u8005<code>700<\/code>\uff08\u53ea\u6709\u62e5\u6709\u8005\u53ef\u4ee5\u6267\u884c\uff09\u3002<\/p>\n\n\n\n<p>\u9664\u4e86\u6267\u884c\u6743\u9650\uff0c\u811a\u672c\u8c03\u7528\u65f6\uff0c\u4e00\u822c\u9700\u8981\u6307\u5b9a\u811a\u672c\u7684\u8def\u5f84\uff08\u6bd4\u5982<code>path\/script.sh<\/code>\uff09\u3002\u5982\u679c\u5c06\u811a\u672c\u653e\u5728\u73af\u5883\u53d8\u91cf<code>$PATH<\/code>\u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\uff0c\u5c31\u4e0d\u9700\u8981\u6307\u5b9a\u8def\u5f84\u4e86\u3002\u56e0\u4e3a Bash \u4f1a\u81ea\u52a8\u5230\u8fd9\u4e9b\u76ee\u5f55\u4e2d\uff0c\u5bfb\u627e\u662f\u5426\u5b58\u5728\u540c\u540d\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n\n\n\n<p>\u5efa\u8bae\u5728\u4e3b\u76ee\u5f55\u65b0\u5efa\u4e00\u4e2a<code>~\/bin<\/code>\u5b50\u76ee\u5f55\uff0c\u4e13\u95e8\u5b58\u653e\u53ef\u6267\u884c\u811a\u672c\uff0c\u7136\u540e\u628a<code>~\/bin<\/code>\u52a0\u5165<code>$PATH<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export PATH=$PATH:~\/bin\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u547d\u4ee4\u6539\u53d8\u73af\u5883\u53d8\u91cf<code>$PATH<\/code>\uff0c\u5c06<code>~\/bin<\/code>\u6dfb\u52a0\u5230<code>$PATH<\/code>\u7684\u672b\u5c3e\u3002\u53ef\u4ee5\u5c06\u8fd9\u4e00\u884c\u52a0\u5230<code>~\/.bashrc<\/code>\u6587\u4ef6\u91cc\u9762\uff0c\u7136\u540e\u91cd\u65b0\u52a0\u8f7d\u4e00\u6b21<code>.bashrc<\/code>\uff0c\u8fd9\u4e2a\u914d\u7f6e\u5c31\u53ef\u4ee5\u751f\u6548\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ source ~\/.bashrc\n<\/code><\/pre>\n\n\n\n<p>\u4ee5\u540e\u4e0d\u7ba1\u5728\u4ec0\u4e48\u76ee\u5f55\uff0c\u76f4\u63a5\u8f93\u5165\u811a\u672c\u6587\u4ef6\u540d\uff0c\u811a\u672c\u5c31\u4f1a\u6267\u884c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ script.sh\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u547d\u4ee4\u6ca1\u6709\u6307\u5b9a\u811a\u672c\u8def\u5f84\uff0c\u56e0\u4e3a<code>script.sh<\/code>\u5728<code>$PATH<\/code>\u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\u3002<\/p>\n\n\n\n<h2>env \u547d\u4ee4<\/h2>\n\n\n\n<p><code>env<\/code>\u547d\u4ee4\u603b\u662f\u6307\u5411<code>\/usr\/bin\/env<\/code>\u6587\u4ef6\uff0c\u6216\u8005\u8bf4\uff0c\u8fd9\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\u603b\u662f\u5728\u76ee\u5f55<code>\/usr\/bin<\/code>\u3002<\/p>\n\n\n\n<p><code>#!\/usr\/bin\/env NAME<\/code>\u8fd9\u4e2a\u8bed\u6cd5\u7684\u610f\u601d\u662f\uff0c\u8ba9 Shell \u67e5\u627e<code>$PATH<\/code>\u73af\u5883\u53d8\u91cf\u91cc\u9762\u7b2c\u4e00\u4e2a\u5339\u914d\u7684<code>NAME<\/code>\u3002\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u67d0\u4e2a\u547d\u4ee4\u7684\u5177\u4f53\u8def\u5f84\uff0c\u6216\u8005\u5e0c\u671b\u517c\u5bb9\u5176\u4ed6\u7528\u6237\u7684\u673a\u5668\uff0c\u8fd9\u6837\u7684\u5199\u6cd5\u5c31\u5f88\u6709\u7528\u3002<\/p>\n\n\n\n<p><code>\/usr\/bin\/env bash<\/code>\u7684\u610f\u601d\u5c31\u662f\uff0c\u8fd4\u56de<code>bash<\/code>\u53ef\u6267\u884c\u6587\u4ef6\u7684\u4f4d\u7f6e\uff0c\u524d\u63d0\u662f<code>bash<\/code>\u7684\u8def\u5f84\u662f\u5728<code>$PATH<\/code>\u91cc\u9762\u3002\u5176\u4ed6\u811a\u672c\u6587\u4ef6\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u547d\u4ee4\u3002\u6bd4\u5982 Node.js \u811a\u672c\u7684 Shebang \u884c\uff0c\u53ef\u4ee5\u5199\u6210\u4e0b\u9762\u8fd9\u6837\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env node\n<\/code><\/pre>\n\n\n\n<p><code>env<\/code>\u547d\u4ee4\u7684\u53c2\u6570\u5982\u4e0b\u3002<\/p>\n\n\n\n<ul><li><code>-i<\/code>, <code>--ignore-environment<\/code>\uff1a\u4e0d\u5e26\u73af\u5883\u53d8\u91cf\u542f\u52a8\u3002<\/li><li><code>-u<\/code>, <code>--unset=NAME<\/code>\uff1a\u4ece\u73af\u5883\u53d8\u91cf\u4e2d\u5220\u9664\u4e00\u4e2a\u53d8\u91cf\u3002<\/li><li><code>--help<\/code>\uff1a\u663e\u793a\u5e2e\u52a9\u3002<\/li><li><code>--version<\/code>\uff1a\u8f93\u51fa\u7248\u672c\u4fe1\u606f\u3002<\/li><\/ul>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u65b0\u5efa\u4e00\u4e2a\u4e0d\u5e26\u4efb\u4f55\u73af\u5883\u53d8\u91cf\u7684 Shell\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ env -i \/bin\/sh\n<\/code><\/pre>\n\n\n\n<h2>\u6ce8\u91ca<\/h2>\n\n\n\n<p>Bash \u811a\u672c\u4e2d\uff0c<code>#<\/code>\u8868\u793a\u6ce8\u91ca\uff0c\u53ef\u4ee5\u653e\u5728\u884c\u9996\uff0c\u4e5f\u53ef\u4ee5\u653e\u5728\u884c\u5c3e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u672c\u884c\u662f\u6ce8\u91ca\necho 'Hello World!'\n\necho 'Hello World!' # \u4e95\u53f7\u540e\u9762\u7684\u90e8\u5206\u4e5f\u662f\u6ce8\u91ca\n<\/code><\/pre>\n\n\n\n<p>\u5efa\u8bae\u5728\u811a\u672c\u5f00\u5934\uff0c\u4f7f\u7528\u6ce8\u91ca\u8bf4\u660e\u5f53\u524d\u811a\u672c\u7684\u4f5c\u7528\uff0c\u8fd9\u6837\u6709\u5229\u4e8e\u65e5\u540e\u7684\u7ef4\u62a4\u3002<\/p>\n\n\n\n<h2>\u811a\u672c\u53c2\u6570<\/h2>\n\n\n\n<p>\u8c03\u7528\u811a\u672c\u7684\u65f6\u5019\uff0c\u811a\u672c\u6587\u4ef6\u540d\u540e\u9762\u53ef\u4ee5\u5e26\u6709\u53c2\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ script.sh word1 word2 word3\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c<code>script.sh<\/code>\u662f\u4e00\u4e2a\u811a\u672c\u6587\u4ef6\uff0c<code>word1<\/code>\u3001<code>word2<\/code>\u548c<code>word3<\/code>\u662f\u4e09\u4e2a\u53c2\u6570\u3002<\/p>\n\n\n\n<p>\u811a\u672c\u6587\u4ef6\u5185\u90e8\uff0c\u53ef\u4ee5\u4f7f\u7528\u7279\u6b8a\u53d8\u91cf\uff0c\u5f15\u7528\u8fd9\u4e9b\u53c2\u6570\u3002<\/p>\n\n\n\n<ul><li><code>$0<\/code>\uff1a\u811a\u672c\u6587\u4ef6\u540d\uff0c\u5373<code>script.sh<\/code>\u3002<\/li><li><code>$1<\/code>~<code>$9<\/code>\uff1a\u5bf9\u5e94\u811a\u672c\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5230\u7b2c\u4e5d\u4e2a\u53c2\u6570\u3002<\/li><li><code>$#<\/code>\uff1a\u53c2\u6570\u7684\u603b\u6570\u3002<\/li><li><code>$@<\/code>\uff1a\u5168\u90e8\u7684\u53c2\u6570\uff0c\u53c2\u6570\u4e4b\u95f4\u4f7f\u7528\u7a7a\u683c\u5206\u9694\u3002<\/li><li><code>$*<\/code>\uff1a\u5168\u90e8\u7684\u53c2\u6570\uff0c\u53c2\u6570\u4e4b\u95f4\u4f7f\u7528\u53d8\u91cf<code>$IFS<\/code>\u503c\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u5206\u9694\uff0c\u9ed8\u8ba4\u4e3a\u7a7a\u683c\uff0c\u4f46\u662f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u3002<\/li><\/ul>\n\n\n\n<p>\u5982\u679c\u811a\u672c\u7684\u53c2\u6570\u591a\u4e8e9\u4e2a\uff0c\u90a3\u4e48\u7b2c10\u4e2a\u53c2\u6570\u53ef\u4ee5\u7528<code>${10}<\/code>\u7684\u5f62\u5f0f\u5f15\u7528\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff0c\u5982\u679c\u547d\u4ee4\u662f<code>command -o foo bar<\/code>\uff0c\u90a3\u4e48<code>-o<\/code>\u662f<code>$1<\/code>\uff0c<code>foo<\/code>\u662f<code>$2<\/code>\uff0c<code>bar<\/code>\u662f<code>$3<\/code>\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u811a\u672c\u5185\u90e8\u8bfb\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n# script.sh\n\necho \"\u5168\u90e8\u53c2\u6570\uff1a\" $@\necho \"\u547d\u4ee4\u884c\u53c2\u6570\u6570\u91cf\uff1a\" $#\necho '$0 = ' $0\necho '$1 = ' $1\necho '$2 = ' $2\necho '$3 = ' $3\n<\/code><\/pre>\n\n\n\n<p>\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ .\/script.sh a b c\n\u5168\u90e8\u53c2\u6570\uff1aa b c\n\u547d\u4ee4\u884c\u53c2\u6570\u6570\u91cf\uff1a3\n$0 =  script.sh\n$1 =  a\n$2 =  b\n$3 =  c\n<\/code><\/pre>\n\n\n\n<p>\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\uff0c\u5229\u7528<code>for<\/code>\u5faa\u73af\uff0c\u53ef\u4ee5\u8bfb\u53d6\u6bcf\u4e00\u4e2a\u53c2\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\nfor i in \"$@\"; do\n  echo $i\ndone\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c<code>$@<\/code>\u8fd4\u56de\u4e00\u4e2a\u5168\u90e8\u53c2\u6570\u7684\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u591a\u4e2a\u53c2\u6570\u653e\u5728\u53cc\u5f15\u53f7\u91cc\u9762\uff0c\u89c6\u4e3a\u4e00\u4e2a\u53c2\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ .\/script.sh \"a b\"\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0cBash \u4f1a\u8ba4\u4e3a<code>\"a b\"<\/code>\u662f\u4e00\u4e2a\u53c2\u6570\uff0c<code>$1<\/code>\u4f1a\u8fd4\u56de<code>a b<\/code>\u3002\u6ce8\u610f\uff0c\u8fd4\u56de\u65f6\u4e0d\u5305\u62ec\u53cc\u5f15\u53f7\u3002<\/p>\n\n\n\n<h2>shift \u547d\u4ee4<\/h2>\n\n\n\n<p><code>shift<\/code>\u547d\u4ee4\u53ef\u4ee5\u6539\u53d8\u811a\u672c\u53c2\u6570\uff0c\u6bcf\u6b21\u6267\u884c\u90fd\u4f1a\u79fb\u9664\u811a\u672c\u5f53\u524d\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff08<code>$1<\/code>\uff09\uff0c\u4f7f\u5f97\u540e\u9762\u7684\u53c2\u6570\u5411\u524d\u4e00\u4f4d\uff0c\u5373<code>$2<\/code>\u53d8\u6210<code>$1<\/code>\u3001<code>$3<\/code>\u53d8\u6210<code>$2<\/code>\u3001<code>$4<\/code>\u53d8\u6210<code>$3<\/code>\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n\n\n\n<p><code>while<\/code>\u5faa\u73af\u7ed3\u5408<code>shift<\/code>\u547d\u4ee4\uff0c\u4e5f\u53ef\u4ee5\u8bfb\u53d6\u6bcf\u4e00\u4e2a\u53c2\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\necho \"\u4e00\u5171\u8f93\u5165\u4e86 $# \u4e2a\u53c2\u6570\"\n\nwhile &#91; \"$1\" != \"\" ]; do\n  echo \"\u5269\u4e0b $# \u4e2a\u53c2\u6570\"\n  echo \"\u53c2\u6570\uff1a$1\"\n  shift\ndone\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c<code>shift<\/code>\u547d\u4ee4\u6bcf\u6b21\u79fb\u9664\u5f53\u524d\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u4ece\u800c\u901a\u8fc7<code>while<\/code>\u5faa\u73af\u904d\u5386\u6240\u6709\u53c2\u6570\u3002<\/p>\n\n\n\n<p><code>shift<\/code>\u547d\u4ee4\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u6574\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u6307\u5b9a\u6240\u8981\u79fb\u9664\u7684\u53c2\u6570\u4e2a\u6570\uff0c\u9ed8\u8ba4\u4e3a<code>1<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>shift 3\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u547d\u4ee4\u79fb\u9664\u524d\u4e09\u4e2a\u53c2\u6570\uff0c\u539f\u6765\u7684<code>$4<\/code>\u53d8\u6210<code>$1<\/code>\u3002<\/p>\n\n\n\n<h2>getopts \u547d\u4ee4<\/h2>\n\n\n\n<p><code>getopts<\/code>\u547d\u4ee4\u7528\u5728\u811a\u672c\u5185\u90e8\uff0c\u53ef\u4ee5\u89e3\u6790\u590d\u6742\u7684\u811a\u672c\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u901a\u5e38\u4e0e<code>while<\/code>\u5faa\u73af\u4e00\u8d77\u4f7f\u7528\uff0c\u53d6\u51fa\u811a\u672c\u6240\u6709\u7684\u5e26\u6709\u524d\u7f6e\u8fde\u8bcd\u7ebf\uff08<code>-<\/code>\uff09\u7684\u53c2\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>getopts optstring name\n<\/code><\/pre>\n\n\n\n<p>\u5b83\u5e26\u6709\u4e24\u4e2a\u53c2\u6570\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570<code>optstring<\/code>\u662f\u5b57\u7b26\u4e32\uff0c\u7ed9\u51fa\u811a\u672c\u6240\u6709\u7684\u8fde\u8bcd\u7ebf\u53c2\u6570\u3002\u6bd4\u5982\uff0c\u67d0\u4e2a\u811a\u672c\u53ef\u4ee5\u6709\u4e09\u4e2a\u914d\u7f6e\u9879\u53c2\u6570<code>-l<\/code>\u3001<code>-h<\/code>\u3001<code>-a<\/code>\uff0c\u5176\u4e2d\u53ea\u6709<code>-a<\/code>\u53ef\u4ee5\u5e26\u6709\u53c2\u6570\u503c\uff0c\u800c<code>-l<\/code>\u548c<code>-h<\/code>\u662f\u5f00\u5173\u53c2\u6570\uff0c\u90a3\u4e48<code>getopts<\/code>\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5199\u6210<code>lha:<\/code>\uff0c\u987a\u5e8f\u4e0d\u91cd\u8981\u3002\u6ce8\u610f\uff0c<code>a<\/code>\u540e\u9762\u6709\u4e00\u4e2a\u5192\u53f7\uff0c\u8868\u793a\u8be5\u53c2\u6570\u5e26\u6709\u53c2\u6570\u503c\uff0c<code>getopts<\/code>\u89c4\u5b9a\u5e26\u6709\u53c2\u6570\u503c\u7684\u914d\u7f6e\u9879\u53c2\u6570\uff0c\u540e\u9762\u5fc5\u987b\u5e26\u6709\u4e00\u4e2a\u5192\u53f7\uff08<code>:<\/code>\uff09\u3002<code>getopts<\/code>\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570<code>name<\/code>\u662f\u4e00\u4e2a\u53d8\u91cf\u540d\uff0c\u7528\u6765\u4fdd\u5b58\u5f53\u524d\u53d6\u5230\u7684\u914d\u7f6e\u9879\u53c2\u6570\uff0c\u5373<code>l<\/code>\u3001<code>h<\/code>\u6216<code>a<\/code>\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while getopts 'lha:' OPTION; do\n  case \"$OPTION\" in\n    l)\n      echo \"linuxconfig\"\n      ;;\n\n    h)\n      echo \"h stands for h\"\n      ;;\n\n    a)\n      avalue=\"$OPTARG\"\n      echo \"The value provided is $OPTARG\"\n      ;;\n    ?)\n      echo \"script usage: $(basename $0) &#91;-l] &#91;-h] &#91;-a somevalue]\" &gt;&amp;2\n      exit 1\n      ;;\n  esac\ndone\nshift \"$(($OPTIND - 1))\"\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c<code>while<\/code>\u5faa\u73af\u4e0d\u65ad\u6267\u884c<code>getopts 'lha:' OPTION<\/code>\u547d\u4ee4\uff0c\u6bcf\u6b21\u6267\u884c\u5c31\u4f1a\u8bfb\u53d6\u4e00\u4e2a\u8fde\u8bcd\u7ebf\u53c2\u6570\uff08\u4ee5\u53ca\u5bf9\u5e94\u7684\u53c2\u6570\u503c\uff09\uff0c\u7136\u540e\u8fdb\u5165\u5faa\u73af\u4f53\u3002\u53d8\u91cf<code>OPTION<\/code>\u4fdd\u5b58\u7684\u662f\uff0c\u5f53\u524d\u5904\u7406\u7684\u90a3\u4e00\u4e2a\u8fde\u8bcd\u7ebf\u53c2\u6570\uff08\u5373<code>l<\/code>\u3001<code>h<\/code>\u6216<code>a<\/code>\uff09\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u4e86\u6ca1\u6709\u6307\u5b9a\u7684\u53c2\u6570\uff08\u6bd4\u5982<code>-x<\/code>\uff09\uff0c\u90a3\u4e48<code>OPTION<\/code>\u7b49\u4e8e<code>?<\/code>\u3002\u5faa\u73af\u4f53\u5185\u4f7f\u7528<code>case<\/code>\u5224\u65ad\uff0c\u5904\u7406\u8fd9\u56db\u79cd\u4e0d\u540c\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u67d0\u4e2a\u8fde\u8bcd\u7ebf\u53c2\u6570\u5e26\u6709\u53c2\u6570\u503c\uff0c\u6bd4\u5982<code>-a foo<\/code>\uff0c\u90a3\u4e48\u5904\u7406<code>a<\/code>\u53c2\u6570\u7684\u65f6\u5019\uff0c\u73af\u5883\u53d8\u91cf<code>$OPTARG<\/code>\u4fdd\u5b58\u7684\u5c31\u662f\u53c2\u6570\u503c\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff0c\u53ea\u8981\u9047\u5230\u4e0d\u5e26\u8fde\u8bcd\u7ebf\u7684\u53c2\u6570\uff0c<code>getopts<\/code>\u5c31\u4f1a\u6267\u884c\u5931\u8d25\uff0c\u4ece\u800c\u9000\u51fa<code>while<\/code>\u5faa\u73af\u3002\u6bd4\u5982\uff0c<code>getopts<\/code>\u53ef\u4ee5\u89e3\u6790<code>command -l foo<\/code>\uff0c\u4f46\u4e0d\u53ef\u4ee5\u89e3\u6790<code>command foo -l<\/code>\u3002\u53e6\u5916\uff0c\u591a\u4e2a\u8fde\u8bcd\u7ebf\u53c2\u6570\u5199\u5728\u4e00\u8d77\u7684\u5f62\u5f0f\uff0c\u6bd4\u5982<code>command -lh<\/code>\uff0c<code>getopts<\/code>\u4e5f\u53ef\u4ee5\u6b63\u786e\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u53d8\u91cf<code>$OPTIND<\/code>\u5728<code>getopts<\/code>\u5f00\u59cb\u6267\u884c\u524d\u662f<code>1<\/code>\uff0c\u7136\u540e\u6bcf\u6b21\u6267\u884c\u5c31\u4f1a\u52a0<code>1<\/code>\u3002\u7b49\u5230\u9000\u51fa<code>while<\/code>\u5faa\u73af\uff0c\u5c31\u610f\u5473\u7740\u8fde\u8bcd\u7ebf\u53c2\u6570\u5168\u90e8\u5904\u7406\u5b8c\u6bd5\u3002\u8fd9\u65f6\uff0c<code>$OPTIND - 1<\/code>\u5c31\u662f\u5df2\u7ecf\u5904\u7406\u7684\u8fde\u8bcd\u7ebf\u53c2\u6570\u4e2a\u6570\uff0c\u4f7f\u7528<code>shift<\/code>\u547d\u4ee4\u5c06\u8fd9\u4e9b\u53c2\u6570\u79fb\u9664\uff0c\u4fdd\u8bc1\u540e\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u7528<code>$1<\/code>\u3001<code>$2<\/code>\u7b49\u5904\u7406\u547d\u4ee4\u7684\u4e3b\u53c2\u6570\u3002<\/p>\n\n\n\n<h2>\u914d\u7f6e\u9879\u53c2\u6570\u7ec8\u6b62\u7b26 <code>--<\/code><\/h2>\n\n\n\n<p><code>-<\/code>\u548c<code>--<\/code>\u5f00\u5934\u7684\u53c2\u6570\uff0c\u4f1a\u88ab Bash \u5f53\u4f5c\u914d\u7f6e\u9879\u89e3\u91ca\u3002\u4f46\u662f\uff0c\u6709\u65f6\u5b83\u4eec\u4e0d\u662f\u914d\u7f6e\u9879\uff0c\u800c\u662f\u5b9e\u4f53\u53c2\u6570\u7684\u4e00\u90e8\u5206\uff0c\u6bd4\u5982\u6587\u4ef6\u540d\u53eb\u505a<code>-f<\/code>\u6216<code>--file<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat -f\n$ cat --file\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u547d\u4ee4\u7684\u539f\u610f\u662f\u8f93\u51fa\u6587\u4ef6<code>-f<\/code>\u548c<code>--file<\/code>\u7684\u5185\u5bb9\uff0c\u4f46\u662f\u4f1a\u88ab Bash \u5f53\u4f5c\u914d\u7f6e\u9879\u89e3\u91ca\u3002<\/p>\n\n\n\n<p>\u8fd9\u65f6\u5c31\u53ef\u4ee5\u4f7f\u7528\u914d\u7f6e\u9879\u53c2\u6570\u7ec8\u6b62\u7b26<code>--<\/code>\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u544a\u8bc9 Bash\uff0c\u5728\u5b83\u540e\u9762\u7684\u53c2\u6570\u5f00\u5934\u7684<code>-<\/code>\u548c<code>--<\/code>\u4e0d\u662f\u914d\u7f6e\u9879\uff0c\u53ea\u80fd\u5f53\u4f5c\u5b9e\u4f53\u53c2\u6570\u89e3\u91ca\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat -- -f\n$ cat -- --file\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u547d\u4ee4\u53ef\u4ee5\u6b63\u786e\u5c55\u793a\u6587\u4ef6<code>-f<\/code>\u548c<code>--file<\/code>\u7684\u5185\u5bb9\uff0c\u56e0\u4e3a\u5b83\u4eec\u653e\u5728<code>--<\/code>\u7684\u540e\u9762\uff0c\u5f00\u5934\u7684<code>-<\/code>\u548c<code>--<\/code>\u5c31\u4e0d\u518d\u5f53\u4f5c\u914d\u7f6e\u9879\u89e3\u91ca\u4e86\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u8981\u786e\u4fdd\u67d0\u4e2a\u53d8\u91cf\u4e0d\u4f1a\u88ab\u5f53\u4f5c\u914d\u7f6e\u9879\u89e3\u91ca\uff0c\u5c31\u8981\u5728\u5b83\u524d\u9762\u653e\u4e0a\u53c2\u6570\u7ec8\u6b62\u7b26<code>--<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls -- $myPath\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u793a\u4f8b\u4e2d\uff0c<code>--<\/code>\u5f3a\u5236\u53d8\u91cf<code>$myPath<\/code>\u53ea\u80fd\u5f53\u4f5c\u5b9e\u4f53\u53c2\u6570\uff08\u5373\u8def\u5f84\u540d\uff09\u89e3\u91ca\u3002\u5982\u679c\u53d8\u91cf\u4e0d\u662f\u8def\u5f84\u540d\uff0c\u5c31\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ myPath=\"-l\"\n$ ls -- $myPath\nls: \u65e0\u6cd5\u8bbf\u95ee'-l': \u6ca1\u6709\u90a3\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf<code>myPath<\/code>\u7684\u503c\u4e3a<code>-l<\/code>\uff0c\u4e0d\u662f\u8def\u5f84\u3002\u4f46\u662f\uff0c<code>--<\/code>\u5f3a\u5236<code>$myPath<\/code>\u53ea\u80fd\u4f5c\u4e3a\u8def\u5f84\u89e3\u91ca\uff0c\u5bfc\u81f4\u62a5\u9519\u201c\u4e0d\u5b58\u5728\u8be5\u8def\u5f84\u201d\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u5b9e\u9645\u7684\u4f8b\u5b50\uff0c\u5982\u679c\u60f3\u5728\u6587\u4ef6\u91cc\u9762\u641c\u7d22<code>--hello<\/code>\uff0c\u8fd9\u65f6\u4e5f\u8981\u4f7f\u7528\u53c2\u6570\u7ec8\u6b62\u7b26<code>--<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ grep -- \"--hello\" example.txt\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u547d\u4ee4\u5728<code>example.txt<\/code>\u6587\u4ef6\u91cc\u9762\uff0c\u641c\u7d22\u5b57\u7b26\u4e32<code>--hello<\/code>\u3002\u8fd9\u4e2a\u5b57\u7b26\u4e32\u662f<code>--<\/code>\u5f00\u5934\uff0c\u5982\u679c\u4e0d\u7528\u53c2\u6570\u7ec8\u6b62\u7b26\uff0c<code>grep<\/code>\u547d\u4ee4\u5c31\u4f1a\u628a<code>--hello<\/code>\u5f53\u4f5c\u914d\u7f6e\u9879\u53c2\u6570\uff0c\u4ece\u800c\u62a5\u9519\u3002<\/p>\n\n\n\n<h2>exit \u547d\u4ee4<\/h2>\n\n\n\n<p><code>exit<\/code>\u547d\u4ee4\u7528\u4e8e\u7ec8\u6b62\u5f53\u524d\u811a\u672c\u7684\u6267\u884c\uff0c\u5e76\u5411 Shell \u8fd4\u56de\u4e00\u4e2a\u9000\u51fa\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ exit\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u547d\u4ee4\u4e2d\u6b62\u5f53\u524d\u811a\u672c\uff0c\u5c06\u6700\u540e\u4e00\u6761\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001\uff0c\u4f5c\u4e3a\u6574\u4e2a\u811a\u672c\u7684\u9000\u51fa\u72b6\u6001\u3002<\/p>\n\n\n\n<p><code>exit<\/code>\u547d\u4ee4\u540e\u9762\u53ef\u4ee5\u8ddf\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u5c31\u662f\u9000\u51fa\u72b6\u6001\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u9000\u51fa\u503c\u4e3a0\uff08\u6210\u529f\uff09\n$ exit 0\n\n# \u9000\u51fa\u503c\u4e3a1\uff08\u5931\u8d25\uff09\n$ exit 1\n<\/code><\/pre>\n\n\n\n<p>\u9000\u51fa\u65f6\uff0c\u811a\u672c\u4f1a\u8fd4\u56de\u4e00\u4e2a\u9000\u51fa\u503c\u3002\u811a\u672c\u7684\u9000\u51fa\u503c\uff0c<code>0<\/code>\u8868\u793a\u6b63\u5e38\uff0c<code>1<\/code>\u8868\u793a\u53d1\u751f\u9519\u8bef\uff0c<code>2<\/code>\u8868\u793a\u7528\u6cd5\u4e0d\u5bf9\uff0c<code>126<\/code>\u8868\u793a\u4e0d\u662f\u53ef\u6267\u884c\u811a\u672c\uff0c<code>127<\/code>\u8868\u793a\u547d\u4ee4\u6ca1\u6709\u53d1\u73b0\u3002\u5982\u679c\u811a\u672c\u88ab\u4fe1\u53f7<code>N<\/code>\u7ec8\u6b62\uff0c\u5219\u9000\u51fa\u503c\u4e3a<code>128 + N<\/code>\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u53ea\u8981\u9000\u51fa\u503c\u975e0\uff0c\u5c31\u8ba4\u4e3a\u6267\u884c\u51fa\u9519\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if &#91; $(id -u) != \"0\" ]; then\n  echo \"\u6839\u7528\u6237\u624d\u80fd\u6267\u884c\u5f53\u524d\u811a\u672c\"\n  exit 1\nfi\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>id -u<\/code>\u547d\u4ee4\u8fd4\u56de\u7528\u6237\u7684 ID\uff0c\u4e00\u65e6\u7528\u6237\u7684 ID \u4e0d\u7b49\u4e8e<code>0<\/code>\uff08\u6839\u7528\u6237\u7684 ID\uff09\uff0c\u811a\u672c\u5c31\u4f1a\u9000\u51fa\uff0c\u5e76\u4e14\u9000\u51fa\u7801\u4e3a<code>1<\/code>\uff0c\u8868\u793a\u8fd0\u884c\u5931\u8d25\u3002<\/p>\n\n\n\n<p><code>exit<\/code>\u4e0e<code>return<\/code>\u547d\u4ee4\u7684\u5dee\u522b\u662f\uff0c<code>return<\/code>\u547d\u4ee4\u662f\u51fd\u6570\u7684\u9000\u51fa\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\u7ed9\u8c03\u7528\u8005\uff0c\u811a\u672c\u4f9d\u7136\u6267\u884c\u3002<code>exit<\/code>\u662f\u6574\u4e2a\u811a\u672c\u7684\u9000\u51fa\uff0c\u5982\u679c\u5728\u51fd\u6570\u4e4b\u4e2d\u8c03\u7528<code>exit<\/code>\uff0c\u5219\u9000\u51fa\u51fd\u6570\uff0c\u5e76\u7ec8\u6b62\u811a\u672c\u6267\u884c\u3002<\/p>\n\n\n\n<h2>\u547d\u4ee4\u6267\u884c\u7ed3\u679c<\/h2>\n\n\n\n<p>\u547d\u4ee4\u6267\u884c\u7ed3\u675f\u540e\uff0c\u4f1a\u6709\u4e00\u4e2a\u8fd4\u56de\u503c\u3002<code>0<\/code>\u8868\u793a\u6267\u884c\u6210\u529f\uff0c\u975e<code>0<\/code>\uff08\u901a\u5e38\u662f<code>1<\/code>\uff09\u8868\u793a\u6267\u884c\u5931\u8d25\u3002\u73af\u5883\u53d8\u91cf<code>$?<\/code>\u53ef\u4ee5\u8bfb\u53d6\u524d\u4e00\u4e2a\u547d\u4ee4\u7684\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<p>\u5229\u7528\u8fd9\u4e00\u70b9\uff0c\u53ef\u4ee5\u5728\u811a\u672c\u4e2d\u5bf9\u547d\u4ee4\u6267\u884c\u7ed3\u679c\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd \/path\/to\/somewhere\nif &#91; \"$?\" = \"0\" ]; then\n  rm *\nelse\n  echo \"\u65e0\u6cd5\u5207\u6362\u76ee\u5f55\uff01\" 1&gt;&amp;2\n  exit 1\nfi\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c<code>cd \/path\/to\/somewhere<\/code>\u8fd9\u4e2a\u547d\u4ee4\u5982\u679c\u6267\u884c\u6210\u529f\uff08\u8fd4\u56de\u503c\u7b49\u4e8e<code>0<\/code>\uff09\uff0c\u5c31\u5220\u9664\u8be5\u76ee\u5f55\u91cc\u9762\u7684\u6587\u4ef6\uff0c\u5426\u5219\u9000\u51fa\u811a\u672c\uff0c\u6574\u4e2a\u811a\u672c\u7684\u8fd4\u56de\u503c\u53d8\u4e3a<code>1<\/code>\uff0c\u8868\u793a\u6267\u884c\u5931\u8d25\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e<code>if<\/code>\u53ef\u4ee5\u76f4\u63a5\u5224\u65ad\u547d\u4ee4\u7684\u6267\u884c\u7ed3\u679c\uff0c\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u4e0a\u9762\u7684\u811a\u672c\u53ef\u4ee5\u6539\u5199\u6210\u4e0b\u9762\u7684\u6837\u5b50\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if cd \/path\/to\/somewhere; then\n  rm *\nelse\n  echo \"Could not change directory! Aborting.\" 1&gt;&amp;2\n  exit 1\nfi\n<\/code><\/pre>\n\n\n\n<p>\u66f4\u7b80\u6d01\u7684\u5199\u6cd5\u662f\u5229\u7528\u4e24\u4e2a\u903b\u8f91\u8fd0\u7b97\u7b26<code>&amp;&amp;<\/code>\uff08\u4e14\uff09\u548c<code>||<\/code>\uff08\u6216\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u7b2c\u4e00\u6b65\u6267\u884c\u6210\u529f\uff0c\u624d\u4f1a\u6267\u884c\u7b2c\u4e8c\u6b65\ncd \/path\/to\/somewhere &amp;&amp; rm *\n\n# \u7b2c\u4e00\u6b65\u6267\u884c\u5931\u8d25\uff0c\u624d\u4f1a\u6267\u884c\u7b2c\u4e8c\u6b65\ncd \/path\/to\/somewhere || exit 1\n<\/code><\/pre>\n\n\n\n<h2>source \u547d\u4ee4<\/h2>\n\n\n\n<p><code>source<\/code>\u547d\u4ee4\u7528\u4e8e\u6267\u884c\u4e00\u4e2a\u811a\u672c\uff0c\u901a\u5e38\u7528\u4e8e\u91cd\u65b0\u52a0\u8f7d\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ source .bashrc\n<\/code><\/pre>\n\n\n\n<p><code>source<\/code>\u547d\u4ee4\u6700\u5927\u7684\u7279\u70b9\u662f\u5728\u5f53\u524d Shell \u6267\u884c\u811a\u672c\uff0c\u4e0d\u50cf\u76f4\u63a5\u6267\u884c\u811a\u672c\u65f6\uff0c\u4f1a\u65b0\u5efa\u4e00\u4e2a\u5b50 Shell\u3002\u6240\u4ee5\uff0c<code>source<\/code>\u547d\u4ee4\u6267\u884c\u811a\u672c\u65f6\uff0c\u4e0d\u9700\u8981<code>export<\/code>\u53d8\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n# test.sh\necho $foo\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u811a\u672c\u8f93\u51fa<code>$foo<\/code>\u53d8\u91cf\u7684\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5f53\u524d Shell \u65b0\u5efa\u4e00\u4e2a\u53d8\u91cf foo\n$ foo=1\n\n# \u6253\u5370\u8f93\u51fa 1\n$ source test.sh\n1\n\n# \u6253\u5370\u8f93\u51fa\u7a7a\u5b57\u7b26\u4e32\n$ bash test.sh\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c\u5f53\u524d Shell \u7684\u53d8\u91cf<code>foo<\/code>\u5e76\u6ca1\u6709<code>export<\/code>\uff0c\u6240\u4ee5\u76f4\u63a5\u6267\u884c\u65e0\u6cd5\u8bfb\u53d6\uff0c\u4f46\u662f<code>source<\/code>\u6267\u884c\u53ef\u4ee5\u8bfb\u53d6\u3002<\/p>\n\n\n\n<p><code>source<\/code>\u547d\u4ee4\u7684\u53e6\u4e00\u4e2a\u7528\u9014\uff0c\u662f\u5728\u811a\u672c\u5185\u90e8\u52a0\u8f7d\u5916\u90e8\u5e93\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\nsource .\/lib.sh\n\nfunction_from_lib\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u811a\u672c\u5728\u5185\u90e8\u4f7f\u7528<code>source<\/code>\u547d\u4ee4\u52a0\u8f7d\u4e86\u4e00\u4e2a\u5916\u90e8\u5e93\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u5728\u811a\u672c\u91cc\u9762\uff0c\u4f7f\u7528\u8fd9\u4e2a\u5916\u90e8\u5e93\u5b9a\u4e49\u7684\u51fd\u6570\u3002<\/p>\n\n\n\n<p><code>source<\/code>\u6709\u4e00\u4e2a\u7b80\u5199\u5f62\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u70b9\uff08<code>.<\/code>\uff09\u6765\u8868\u793a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ . .bashrc\n<\/code><\/pre>\n\n\n\n<h2>\u522b\u540d\uff0calias \u547d\u4ee4<\/h2>\n\n\n\n<p><code>alias<\/code>\u547d\u4ee4\u7528\u6765\u4e3a\u4e00\u4e2a\u547d\u4ee4\u6307\u5b9a\u522b\u540d\uff0c\u8fd9\u6837\u66f4\u4fbf\u4e8e\u8bb0\u5fc6\u3002\u4e0b\u9762\u662f<code>alias<\/code>\u7684\u683c\u5f0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>alias NAME=DEFINITION\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u547d\u4ee4\u4e2d\uff0c<code>NAME<\/code>\u662f\u522b\u540d\u7684\u540d\u79f0\uff0c<code>DEFINITION<\/code>\u662f\u522b\u540d\u5bf9\u5e94\u7684\u539f\u59cb\u547d\u4ee4\u3002\u6ce8\u610f\uff0c\u7b49\u53f7\u4e24\u4fa7\u4e0d\u80fd\u6709\u7a7a\u683c\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<p>\u4e00\u4e2a\u5e38\u89c1\u7684\u4f8b\u5b50\u662f\u4e3a<code>grep<\/code>\u547d\u4ee4\u8d77\u4e00\u4e2a<code>search<\/code>\u7684\u522b\u540d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>alias search=grep\n<\/code><\/pre>\n\n\n\n<p><code>alias<\/code>\u4e5f\u53ef\u4ee5\u7528\u6765\u4e3a\u957f\u547d\u4ee4\u6307\u5b9a\u4e00\u4e2a\u66f4\u77ed\u7684\u522b\u540d\u3002\u4e0b\u9762\u662f\u901a\u8fc7\u522b\u540d\u5b9a\u4e49\u4e00\u4e2a<code>today<\/code>\u7684\u547d\u4ee4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ alias today='date +\"%A, %B %-d, %Y\"'\n$ today\n\u661f\u671f\u4e00, \u4e00\u6708 6, 2020\n<\/code><\/pre>\n\n\n\n<p>\u6709\u65f6\u4e3a\u4e86\u9632\u6b62\u8bef\u5220\u9664\u6587\u4ef6\uff0c\u53ef\u4ee5\u6307\u5b9a<code>rm<\/code>\u547d\u4ee4\u7684\u522b\u540d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ alias rm='rm -i'\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u547d\u4ee4\u6307\u5b9a<code>rm<\/code>\u547d\u4ee4\u662f<code>rm -i<\/code>\uff0c\u6bcf\u6b21\u5220\u9664\u6587\u4ef6\u4e4b\u524d\uff0c\u90fd\u4f1a\u8ba9\u7528\u6237\u786e\u8ba4\u3002<\/p>\n\n\n\n<p><code>alias<\/code>\u5b9a\u4e49\u7684\u522b\u540d\u4e5f\u53ef\u4ee5\u63a5\u53d7\u53c2\u6570\uff0c\u53c2\u6570\u4f1a\u76f4\u63a5\u4f20\u5165\u539f\u59cb\u547d\u4ee4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ alias echo='echo It says: '\n$ echo hello world\nIt says: hello world\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c\u522b\u540d\u5b9a\u4e49\u4e86<code>echo<\/code>\u547d\u4ee4\u7684\u524d\u4e24\u4e2a\u53c2\u6570\uff0c\u7b49\u540c\u4e8e\u4fee\u6539\u4e86<code>echo<\/code>\u547d\u4ee4\u7684\u9ed8\u8ba4\u884c\u4e3a\u3002<\/p>\n\n\n\n<p>\u6307\u5b9a\u522b\u540d\u4ee5\u540e\uff0c\u5c31\u53ef\u4ee5\u50cf\u4f7f\u7528\u5176\u4ed6\u547d\u4ee4\u4e00\u6837\u4f7f\u7528\u522b\u540d\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u90fd\u4f1a\u628a\u5e38\u7528\u7684\u522b\u540d\u5199\u5728<code>~\/.bashrc<\/code>\u7684\u672b\u5c3e\u3002\u53e6\u5916\uff0c\u53ea\u80fd\u4e3a\u547d\u4ee4\u5b9a\u4e49\u522b\u540d\uff0c\u4e3a\u5176\u4ed6\u90e8\u5206\uff08\u6bd4\u5982\u5f88\u957f\u7684\u8def\u5f84\uff09\u5b9a\u4e49\u522b\u540d\u662f\u65e0\u6548\u7684\u3002<\/p>\n\n\n\n<p>\u76f4\u63a5\u8c03\u7528<code>alias<\/code>\u547d\u4ee4\uff0c\u53ef\u4ee5\u663e\u793a\u6240\u6709\u522b\u540d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ alias\n<\/code><\/pre>\n\n\n\n<p><code>unalias<\/code>\u547d\u4ee4\u53ef\u4ee5\u89e3\u9664\u522b\u540d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ unalias lt\n<\/code><\/pre>\n\n\n\n<h2>\u53c2\u8003\u94fe\u63a5<\/h2>\n\n\n\n<ul><li><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/linuxconfig.org\/how-to-use-getopts-to-parse-a-script-options\">How to use getopts to parse a script options<\/a>, Egidio Docile<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p># Bash \u811a\u672c\u5165\u95e8 \u811a\u672c\uff08script\uff09\u5c31\u662f\u5305\u542b\u4e00\u7cfb\u5217\u547d\u4ee4\u7684\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u3002Shell \u8bfb\u53d6\u8fd9\u4e2a\u6587\u4ef6\uff0c\u4f9d\u6b21\u6267 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/08\/08\/bash-jiaoben\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Bash \u811a\u672c\u5165\u95e8<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/5336"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=5336"}],"version-history":[{"count":8,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/5336\/revisions"}],"predecessor-version":[{"id":5344,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/5336\/revisions\/5344"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=5336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=5336"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=5336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}