{"id":16613,"date":"2024-11-18T13:58:47","date_gmt":"2024-11-18T05:58:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/16613\/"},"modified":"2024-11-18T13:58:47","modified_gmt":"2024-11-18T05:58:47","slug":"%e4%bd%a0%e5%ba%94%e8%af%a5%e7%9f%a5%e9%81%93%e7%9a%84shell-%e8%84%9a%e6%9c%ac%e7%9a%84%e7%bb%8f%e5%85%b8%e5%8d%81%e4%b8%89%e9%97%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/16613\/","title":{"rendered":"\u4f60\u5e94\u8be5\u77e5\u9053\u7684Shell \u811a\u672c\u7684\u7ecf\u5178\u5341\u4e09\u95ee"},"content":{"rendered":"<p> <\/p>\n<p style=\"text-align:center\"><strong><span style=\"font-family: mp-quote, -apple-system-font, BlinkMacSystemFont,\"><strong><span style=\"letter-spacing: 1px\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/275\/013\/5e9e2e09cb66ac70555a8b09900a7da1-0.png\" class=\"aligncenter\" title=\"\u4f60\u5e94\u8be5\u77e5\u9053\u7684Shell \u811a\u672c\u7684\u7ecf\u5178\u5341\u4e09\u95ee\u63d2\u56fe\" alt=\"\u4f60\u5e94\u8be5\u77e5\u9053\u7684Shell \u811a\u672c\u7684\u7ecf\u5178\u5341\u4e09\u95ee\u63d2\u56fe\" \/><\/span><\/strong><\/span><\/strong><\/p>\n<ul>\n<li>\n<p style=\"line-height: 25px\">\u7ecf\u5178\u7684Shell\u5341\u4e09\u95ee<\/p>\n<\/li>\n<ul>\n<li>\n<p style=\"line-height: 25px\">1. \u4e3a\u4f55\u53eb\u505ashell?<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">2. Shell prompt(PS1)\u4e0eCarriage Return(CR)\u7684\u5173\u7cfb?<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">3. \u522b\u4ebaecho, \u4f60\u4e5fecho, \u662f\u95eeecho\u77e5\u591a\u5c11?<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">4. &#8220;&#8221;(\u53cc\u5f15\u53f7)\u4e0e(\u5355\u5f15\u53f7)\u6709\u4ec0\u4e48\u533a\u522b?<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">5. var=value? export\u524d\u540e\u5dee\u5728\u54ea\uff1f<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">6. exec\u8ddfsource\u5dee\u5728\u54ea\uff1f<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">7. ( ) \u4e0e { } \u5dee\u5728\u54ea\uff1f<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">8.<span>&nbsp;<\/span><span style=\"cursor: pointer;line-height: 25px\"><span style=\"line-height: 25px\"> \u4e0e <\/span><\/span>() \u8fd8\u6709 ${} \u5dee\u5728\u54ea\uff1f<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">9.<span>&nbsp;<\/span><span style=\"cursor: pointer;line-height: 25px\"> \u4e0e<\/span>* \u533a\u522b\u5728\u54ea\uff1f<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">10. &amp;&amp; \u4e0e || \u5dee\u5728\u54ea\uff1f<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">11. &gt; \u4e0e <\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">12. \u4f60\u8981if\u8fd8\u662fcase\u5462\uff1f<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">13. for what? while\u4e0euntil\u5dee\u5728\u54ea\uff1f<\/p>\n<\/li>\n<\/ul>\n<\/ul>\n<h3>1. \u4e3a\u4f55\u53eb\u505ashell?<\/h3>\n<blockquote style=\"margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light,\">\n<p style=\"padding-top: 8px;padding-bottom: 8px;font-size: 16px;color: black;line-height: 26px\">\u6211\u4eec\u77e5\u9053\u8ba1\u7b97\u673a\u7684\u8fd0\u4f5c\u4e0d\u80fd\u79bb\u5f00\u786c\u4ef6\uff0c\u4f46\u4f7f\u7528\u8005\u5374\u65e0\u6cd5\u76f4\u63a5\u64cd\u4f5c\u786c\u4ef6\uff0c\u786c\u4ef6\u7684\u9a71\u52a8\u53ea\u80fd\u901a\u8fc7\u4e00\u79cd\u79f0\u4e3a\u64cd\u4f5c\u7cfb\u7edf(OS\uff0cOpertating System)\u7684\u8f6f\u4ef6\u6765\u7ba1\u63a7\u3002linux\u4e25\u683c\u6765\u8bf4\u53ea\u662f\u4e00\u4e2a\u64cd\u4f5c\u7cfb\u7edf(OS)\uff0c\u6211\u4eec\u79f0\u4e4b\u4e3a\u5185\u6838(kernel)\u3002<\/p>\n<\/blockquote>\n<blockquote style=\"margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light,\">\n<p style=\"padding-top: 8px;padding-bottom: 8px;font-size: 16px;color: black;line-height: 26px\">\u4f7f\u7528\u8005\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u64cd\u4f5c\u4e00\u4e2akernel\uff0c\u800c\u662f\u901a\u8fc7kernel\u7684\u5916\u58f3\u7a0b\u5e8f\uff0c\u4e5f\u5c31\u662f\u6240\u8c13\u7684shell\uff0c\u6765\u4e0ekernel\u6c9f\u901a\u3002shell\u662f\u4e00\u4e2a\u4f7f\u7528\u8005\u4e0e\u7cfb\u7edf\u7684\u4ea4\u4e92\u754c\u9762(interface), \u53ea\u80fd\u8ba9\u4f7f\u7528\u8005\u901a\u8fc7\u547d\u4ee4\u884c(command line)\u6765\u4f7f\u7528\u7cfb\u7edf\u6765\u5b8c\u6210\u5de5\u4f5c\u3002\u56e0\u6b64 \uff0c<br \/>shell\u6700\u7b80\u5355\u7684\u5b9a\u4e49\u5c31\u662f\u2014\u2014\u547d\u4ee4\u89e3\u8bd1\u5668(Command Interpreter):<\/p>\n<\/blockquote>\n<ul>\n<li> \u5c06\u4f7f\u7528\u8005\u7684\u547d\u4ee4\u7ffb\u8bd1\u7ed9\u6838\u5fc3\u5904\u7406\uff1b <\/li>\n<li> \u540c\u65f6\uff0c\u5c06\u6838\u5fc3\u5904\u7406\u7ed3\u679c\u7ffb\u8bd1\u7ed9\u4f7f\u7528\u8005\u3002 <\/li>\n<\/ul>\n<blockquote style=\"margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light,\">\n<p style=\"padding-top: 8px;padding-bottom: 8px;font-size: 16px;color: black;line-height: 26px\">\u4e0d\u540c\u7684OS\u4f7f\u7528\u4e0d\u540c\u7684kernel;<br \/>\u540c\u4e00\u4e2akernel\u4e4b\u4e0a\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684shell<br \/>\u5e38\u89c1\u7684shell\u6709sh; bash; csh; ksh;\u7b49<\/p>\n<\/blockquote>\n<h3>2. Shell prompt(PS1)\u4e0eCarriage Return(CR)\u7684\u5173\u7cfb?<\/h3>\n<blockquote style=\"margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light,\">\n<p style=\"padding-top: 8px;padding-bottom: 8px;font-size: 16px;color: black;line-height: 26px\">\u6210\u529f\u767b\u5f55\u4e00\u4e2ashell\u7ec8\u7aef\u540e\uff0c\u6e38\u6807cursor\u5de6\u8fb9\u90e8\u5206\uff0c\u79f0\u4e4b\u4e3a\u63d0\u793a\u7b26prompt<br \/>\u901a\u5e38\u4e00\u822c\u7528\u6237\u4f7f\u7528$\uff0c\u7ba1\u7406\u5458\u7528\u6237root\u4f7f\u7528#<\/p>\n<\/blockquote>\n<ul>\n<li> shell prompt:\u53ef\u4ee5\u8f93\u5165\u547d\u4ee4\u4e86 <br \/>\u952e\u5165\u547d\u4ee4\u540e\uff0c\u76f4\u5230\u8bfb\u8fdbCR(Carriage Return)\u5b57\u7b26\u4e3a\u6b62 <\/li>\n<li> Carriage Return:\u53ef\u4ee5\u6267\u884c\u547d\u4ee4\u4e86 <\/li>\n<\/ul>\n<blockquote style=\"margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light,\">\n<p style=\"padding-top: 8px;padding-bottom: 8px;font-size: 16px;color: black;line-height: 26px\">\u82e5\u4ece\u6280\u672f\u7684\u7ec6\u8282\u6765\u770b\uff0cshell\u4f1a\u4f9d\u636eIFS(Internal Field Seperator) \u5c06command line\u6240\u8f93\u5165\u7684\u6587\u5b57\u62c6\u89e3\u4e3a&#8221;\u5b57\u6bb5&#8221;(word\/field)\u3002\u7136\u540e\u518d\u9488\u5bf9\u7279\u6b8a\u5b57\u7b26(meta)\u5148\u4f5c\u5904\u7406\uff0c\u6700\u540e\u91cd\u7ec4\u6574\u884ccommand line\u3002<\/p>\n<\/blockquote>\n<h3>3. \u522b\u4ebaecho, \u4f60\u4e5fecho, \u662f\u95eeecho\u77e5\u591a\u5c11?<\/h3>\n<blockquote style=\"margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light,\">\n<p style=\"padding-top: 8px;padding-bottom: 8px;font-size: 16px;color: black;line-height: 26px\">echo\u5c06argument\u9001\u5230\u6807\u51c6\u8f93\u51fa(stdout)\uff0c\u901a\u5e38\u663e\u793a\u5728\u5c4f\u5e55<\/p>\n<\/blockquote>\n<ul>\n<li> stdin \u6807\u51c6\u8f93\u5165 <\/li>\n<li> stdout \u6807\u51c6\u8f93\u51fa <\/li>\n<li> stderr \u6807\u51c6\u9519\u8bef\u8f93\u51fa <\/li>\n<\/ul>\n<pre>echo&nbsp;-n&nbsp;&nbsp;#&nbsp;\u53d6\u6d88\u6362\u884c\u7b26\necho&nbsp;-e&nbsp;&nbsp;#&nbsp;\u542f\u7528\u53cd\u659c\u6760\u8f6c\u8bd1<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>4. &#8220;&#8221;(\u53cc\u5f15\u53f7)\u4e0e(\u5355\u5f15\u53f7)\u6709\u4ec0\u4e48\u533a\u522b?<\/h3>\n<ul>\n<li> hard quote:&#8221;(\u5355\u5f15\u53f7),\u5173\u95ed\u6240\u6709\u5f15\u7528 <\/li>\n<li> soft quote:&#8221;&#8221;(\u53cc\u5f15\u53f7),\u4fdd\u7559$\u5f15\u7528 <\/li>\n<\/ul>\n<h3>5. var=value? export\u524d\u540e\u5dee\u5728\u54ea\uff1f<\/h3>\n<blockquote style=\"margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light,\">\n<p style=\"padding-top: 8px;padding-bottom: 8px;font-size: 16px;color: black;line-height: 26px\">\u53d8\u91cf\u5b9a\u4e49\uff1aname=value\uff0c\u7b49\u53f7\u5de6\u53f3\u4e24\u8fb9\u4e0d\u80fd\u4f7f\u7528\u5206\u9694\u7b26\u3002<br \/>\u53d8\u91cf\u66ff\u6362\uff1aecho ${name}<br \/>export\u53d8\u91cf\uff1aexport name=value\uff0c\u4f7f\u53d8\u91cf\u6210\u4e3a\u73af\u5883\u53d8\u91cf<\/p>\n<\/blockquote>\n<pre>#&nbsp;\u672c\u5730\u53d8\u91cf\nA=B\n#&nbsp;\u53d6\u6d88\u53d8\u91cf\nunset&nbsp;A\n#&nbsp;\u73af\u5883\u53d8\u91cf\nexport&nbsp;A=B<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>6. exec\u8ddfsource\u5dee\u5728\u54ea\uff1f<\/h3>\n<blockquote style=\"margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light,\">\n<p style=\"padding-top: 8px;padding-bottom: 8px;font-size: 16px;color: black;line-height: 26px\">\u73af\u5883\u53d8\u91cf\u53ea\u80fd\u4ece\u7236\u8fdb\u7a0b\u5230\u5b50\u8fdb\u7a0b\u5355\u5411\u4f20\u9012\u3002\u6362\u53e5\u8bdd\u8bf4\uff1a\u5728\u5b50\u8fdb\u7a0b\u4e2d\u73af\u5883\u5982\u4f55\u53d8\u66f4\uff0c\u5747\u4e0d\u4f1a\u5f71\u54cd\u7236\u8fdb\u7a0b\u7684\u73af\u5883\u3002<br \/>\u5f53\u6211\u4eec\u6267\u884c\u4e00\u4e2ashell script\u65f6\uff0c\u5176\u5b9e\u662f\u5148\u4ea7\u751f\u4e00\u4e2asub-shell\u7684\u5b50\u8fdb\u7a0b\uff0c \u7136\u540esub-shell\u518d\u53bb\u4ea7\u751f\u547d\u4ee4\u884c\u7684\u5b50\u8fdb\u7a0b\u3002\u53e6\u5916\uff0c\u641c\u7d22\u516c\u4f17\u53f7Linux\u5c31\u8be5\u8fd9\u6837\u5b66\u540e\u53f0\u56de\u590d\u201cgit\u4e66\u7c4d\u201d\uff0c\u83b7\u53d6\u4e00\u4efd\u60ca\u559c\u793c\u5305\u3002<\/p>\n<\/blockquote>\n<pre>#&nbsp;\u521b\u5efa\u5b50shell\u6267\u884c\u811a\u672c\n.\/sh\n#&nbsp;\u5f53\u524dshell\u6267\u884c\nsource&nbsp;sh\n#&nbsp;\u5f53\u524dshell\u6267\u884c\u540e\u9000\u51fa\nexec&nbsp;sh<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>7. ( ) \u4e0e { } \u5dee\u5728\u54ea\uff1f<\/h3>\n<p style=\"padding-top: 8px;padding-bottom: 8px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light,\">( )<span>&nbsp;<\/span>\u5c06command group\u7f6e\u4e8esub-shell\u6267\u884c<br \/>{ }<span>&nbsp;<\/span>\u5219\u662f\u5728\u540c\u4e00\u4e2ashell\u5185\u5b8c\u6210<\/p>\n<h3>8.<span>&nbsp;<\/span><span style=\"cursor: pointer;line-height: 32px\"><span style=\"line-height: 32px\"> \u4e0e <\/span><\/span>() \u8fd8\u6709 ${} \u5dee\u5728\u54ea\uff1f<\/h3>\n<ul>\n<li> $()\u4e0e &#8220;(\u53cd\u5f15\u53f7) \u90fd\u662f\u7528\u6765\u505a\u547d\u4ee4\u66ff\u6362\u7528\u7684\u3002 <\/li>\n<li> ${var}\u4e0e <span>&nbsp;<\/span>$var <span>&nbsp;<\/span>\u90fd\u662f\u7528\u6765\u505a\u53d8\u91cf\u66ff\u6362\u7528\u7684\u3002 <\/li>\n<\/ul>\n<pre>#&nbsp;\u5047\u8bbe\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cf\u4e3a\uff1a\nfile=\/dir1\/dir2\/dir3\/my.file.txt\n#&nbsp;\u6211\u4eec\u53ef\u4ee5\u7528&nbsp;${&nbsp;}&nbsp;\u5206\u522b\u66ff\u6362\u83b7\u5f97\u4e0d\u540c\u7684\u503c\uff1a\n#&nbsp;&nbsp;shell\u5b57\u7b26\u4e32\u7684\u975e\u8d2a\u5a6a(\u6700\u5c0f\u5339\u914d)\u5de6\u5220\u9664\n${file#*\/}&nbsp;#&nbsp;\u62ff\u6389\u7b2c\u4e00\u6761&nbsp;\/&nbsp;\u53ca\u5176\u5de6\u8fb9\u7684\u5b57\u7b26\u4e32\uff1adir1\/dir2\/dir3\/my.file.txt\n${file#*.}&nbsp;#\u62ff\u6389\u7b2c\u4e00\u4e2a&nbsp;.&nbsp;\u53ca\u5176\u5de6\u8fb9\u7684\u5b57\u7b26\u4e32\uff1afile.txt\n#&nbsp;&nbsp;shell\u5b57\u7b26\u4e32\u7684\u8d2a\u5a6a(\u6700\u5927\u5339\u914d)\u5de6\u5220\u9664\n${file##*\/}&nbsp;#&nbsp;\u62ff\u6389\u6700\u540e\u4e00\u6761&nbsp;\/&nbsp;\u53ca\u5176\u5de6\u8fb9\u7684\u5b57\u7b26\u4e32\uff1amy.file.txt\n${file##*.}&nbsp;#&nbsp;\u62ff\u6389\u6700\u540e\u4e00\u4e2a&nbsp;.&nbsp;\u53ca\u5176\u5de6\u8fb9\u7684\u5b57\u7b26\u4e32\uff1atxt\n#&nbsp;&nbsp;shell\u5b57\u7b26\u4e32\u7684\u975e\u8d2a\u5a6a(\u6700\u5c0f\u5339\u914d)\u53f3\u5220\u9664\uff1a\n${file%\/*}&nbsp;#&nbsp;\u62ff\u6389\u6700\u540e\u6761&nbsp;\/&nbsp;\u53ca\u5176\u53f3\u8fb9\u7684\u5b57\u7b26\u4e32\uff1a\/dir1\/dir2\/dir3\n${file%.*}&nbsp;#&nbsp;\u62ff\u6389\u6700\u540e\u4e00\u4e2a&nbsp;.&nbsp;\u53ca\u5176\u53f3\u8fb9\u7684\u5b57\u7b26\u4e32\uff1a\/dir1\/dir2\/dir3\/my.file\n#&nbsp;&nbsp;shell\u5b57\u7b26\u4e32\u7684\u8d2a\u5a6a(\u6700\u5927\u5339\u914d)\u53f3\u5220\u9664\uff1a\n${file%%\/*}&nbsp;#&nbsp;\u62ff\u6389\u7b2c\u4e00\u6761&nbsp;\/&nbsp;\u53ca\u5176\u53f3\u8fb9\u7684\u5b57\u7b26\u4e32\uff1a(\u7a7a\u503c)\n${file%%.*}&nbsp;#&nbsp;\u62ff\u6389\u7b2c\u4e00\u4e2a&nbsp;.&nbsp;\u53ca\u5176\u53f3\u8fb9\u7684\u5b57\u7b26\u4e32\uff1a\/dir1\/dir2\/dir3\/my\n\n\u8bb0\u5fc6\u7684\u65b9\u6cd5\u4e3a\uff1a\n#&nbsp;\u662f\u53bb\u6389\u5de6\u8fb9(\u5728\u952e\u76d8\u4e0a&nbsp;#&nbsp;\u5728&nbsp;$&nbsp;\u4e4b\u5de6\u8fb9)\n%&nbsp;\u662f\u53bb\u6389\u53f3\u8fb9(\u5728\u952e\u76d8\u4e0a&nbsp;%&nbsp;\u5728&nbsp;$&nbsp;\u4e4b\u53f3\u8fb9)\n\u5355\u4e00\u7b26\u53f7\u662f\u6700\u5c0f\u5339\u914d\ufe54\u4e24\u4e2a\u7b26\u53f7\u662f\u6700\u5927\u5339\u914d\u3002<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>#&nbsp;&nbsp;shell\u5b57\u7b26\u4e32\u53d6\u5b50\u4e32\uff1a\n${file:0:5}\uff1a\u63d0\u53d6\u6700\u5de6\u8fb9\u7684&nbsp;5&nbsp;\u4e2a\u5b57\u8282\uff1a\/dir1\n${file:5:5}\uff1a\u63d0\u53d6\u7b2c&nbsp;5&nbsp;\u4e2a\u5b57\u8282\u53f3\u8fb9\u7684\u8fde\u7eed&nbsp;5&nbsp;\u4e2a\u5b57\u8282\uff1a\/dir2\n#&nbsp;&nbsp;shell\u5b57\u7b26\u4e32\u53d8\u91cf\u503c\u7684\u66ff\u6362\uff1a\n${file\/dir\/path}\uff1a\u5c06\u7b2c\u4e00\u4e2a&nbsp;dir&nbsp;\u63d0\u6362\u4e3a&nbsp;path\uff1a\/path1\/dir2\/dir3\/my.file.txt\n${file\/\/dir\/path}\uff1a\u5c06\u5168\u90e8&nbsp;dir&nbsp;\u63d0\u6362\u4e3a&nbsp;path\uff1a\/path1\/path2\/path3\/my.file.txt\n#&nbsp;&nbsp;${}\u8fd8\u53ef\u9488\u5bf9\u53d8\u91cf\u7684\u4e0d\u540c\u72b6\u6001(\u6ca1\u8bbe\u5b9a\u3001\u7a7a\u503c\u3001\u975e\u7a7a\u503c)\u8fdb\u884c\u8d4b\u503c\uff1a\n${file-my.file.txt}&nbsp;\uff1a\u5047\u5982&nbsp;$file&nbsp;\u6ca1\u6709\u8bbe\u5b9a\uff0c\u5219\u4f7f\u7528&nbsp;my.file.txt&nbsp;\u4f5c\u4f20\u56de\u503c\u3002(\u7a7a\u503c\u53ca\u975e\u7a7a\u503c\u65f6\u4e0d\u4f5c\u5904\u7406)&nbsp;\n${file:-my.file.txt}&nbsp;\uff1a\u5047\u5982&nbsp;$file&nbsp;\u6ca1\u6709\u8bbe\u5b9a\u6216\u4e3a\u7a7a\u503c\uff0c\u5219\u4f7f\u7528&nbsp;my.file.txt&nbsp;\u4f5c\u4f20\u56de\u503c\u3002&nbsp;(\u975e\u7a7a\u503c\u65f6\u4e0d\u4f5c\u5904\u7406)\n${file+my.file.txt}&nbsp;\uff1a\u5047\u5982&nbsp;$file&nbsp;\u8bbe\u4e3a\u7a7a\u503c\u6216\u975e\u7a7a\u503c\uff0c\u5747\u4f7f\u7528&nbsp;my.file.txt&nbsp;\u4f5c\u4f20\u56de\u503c\u3002(\u6ca1\u8bbe\u5b9a\u65f6\u4e0d\u4f5c\u5904\u7406)\n${file:+my.file.txt}&nbsp;\uff1a\u82e5&nbsp;$file&nbsp;\u4e3a\u975e\u7a7a\u503c\uff0c\u5219\u4f7f\u7528&nbsp;my.file.txt&nbsp;\u4f5c\u4f20\u56de\u503c\u3002&nbsp;(\u6ca1\u8bbe\u5b9a\u53ca\u7a7a\u503c\u65f6\u4e0d\u4f5c\u5904\u7406)\n${file=my.file.txt}&nbsp;\uff1a\u82e5&nbsp;$file&nbsp;\u6ca1\u8bbe\u5b9a\uff0c\u5219\u4f7f\u7528&nbsp;my.file.txt&nbsp;\u4f5c\u4f20\u56de\u503c\uff0c\u540c\u65f6\u5c06&nbsp;$file&nbsp;\u8d4b\u503c\u4e3a&nbsp;my.file.txt&nbsp;\u3002&nbsp;(\u7a7a\u503c\u53ca\u975e\u7a7a\u503c\u65f6\u4e0d\u4f5c\u5904\u7406)\n${file:=my.file.txt}&nbsp;\uff1a\u82e5&nbsp;$file&nbsp;\u6ca1\u8bbe\u5b9a\u6216\u4e3a\u7a7a\u503c\uff0c\u5219\u4f7f\u7528&nbsp;my.file.txt&nbsp;\u4f5c\u4f20\u56de\u503c\uff0c\u540c\u65f6\u5c06&nbsp;$file&nbsp;\u8d4b\u503c\u4e3a&nbsp;my.file.txt&nbsp;\u3002&nbsp;(\u975e\u7a7a\u503c\u65f6\u4e0d\u4f5c\u5904\u7406)\n${file?my.file.txt}&nbsp;\uff1a\u82e5&nbsp;$file&nbsp;\u6ca1\u8bbe\u5b9a\uff0c\u5219\u5c06&nbsp;my.file.txt&nbsp;\u8f93\u51fa\u81f3&nbsp;STDERR\u3002&nbsp;(\u7a7a\u503c\u53ca\u975e\u7a7a\u503c\u65f6\u4e0d\u4f5c\u5904\u7406)\n${file:?my.file.txt}&nbsp;\uff1a\u82e5&nbsp;$file&nbsp;\u6ca1\u8bbe\u5b9a\u6216\u4e3a\u7a7a\u503c\uff0c\u5219\u5c06&nbsp;my.file.txt&nbsp;\u8f93\u51fa\u81f3&nbsp;STDERR\u3002&nbsp;(\u975e\u7a7a\u503c\u65f6\u4e0d\u4f5c\u5904\u7406)\n\ntips:\n\u4ee5\u4e0a\u7684\u7406\u89e3\u5728\u4e8e,&nbsp;\u4f60\u4e00\u5b9a\u8981\u5206\u6e05\u695a&nbsp;unset&nbsp;\u4e0e&nbsp;null&nbsp;\u53ca&nbsp;non-null&nbsp;\u8fd9\u4e09\u79cd\u8d4b\u503c\u72b6\u6001.\n\u4e00\u822c\u800c\u8a00,&nbsp;:&nbsp;\u4e0e&nbsp;null&nbsp;\u6709\u5173,&nbsp;\u82e5\u4e0d\u5e26&nbsp;:&nbsp;\u7684\u8bdd,&nbsp;null&nbsp;\u4e0d\u53d7\u5f71\u54cd,&nbsp;\u82e5\u5e26&nbsp;:&nbsp;\u5219\u8fde&nbsp;null&nbsp;\u4e5f\u53d7\u5f71\u54cd.<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>#&nbsp;&nbsp;\u8ba1\u7b97shell\u5b57\u7b26\u4e32\u53d8\u91cf\u7684\u957f\u5ea6\uff1a${#var}\n${#var}&nbsp;\u53ef\u8ba1\u7b97\u51fa\u53d8\u91cf\u503c\u7684\u957f\u5ea6\uff1a\n${#file}&nbsp;\u53ef\u5f97\u5230&nbsp;27&nbsp;\uff0c\u56e0\u4e3a&nbsp;\/dir1\/dir2\/dir3\/my.file.txt&nbsp;\u521a\u597d\u662f&nbsp;27&nbsp;\u4e2a\u5b57\u8282...\n#&nbsp;&nbsp;bash\u6570\u7ec4(array)\u7684\u5904\u7406\u65b9\u6cd5\n\u6570\u7ec4:\nA=(a&nbsp;b&nbsp;c&nbsp;d)\n\u5f15\u7528\u6570\u7ec4:\n${A[@]}\n${A[*]}\n\u8bbf\u95ee\u6570\u7ec4\u6210\u5458\n${A[0]}\n\u8ba1\u7b97\u6570\u7ec4\u957f\u5ea6\n${#A[@]}\n${#A[*]}\n\u6570\u7ec4\u91cd\u65b0\u8d4b\u503c\nA[2]=xyz\n#&nbsp;$((&nbsp;))\u662f\u7528\u6765\u505a\u6574\u6570\u8fd0\u7b97\u7684&nbsp;\na=5;b=7;c=2;\necho&nbsp;$((&nbsp;a&nbsp;+&nbsp;b&nbsp;*&nbsp;c))<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>9.<span>&nbsp;<\/span><span style=\"cursor: pointer;line-height: 32px\"> \u4e0e<\/span>* \u533a\u522b\u5728\u54ea\uff1f<\/h3>\n<ul>\n<li> &#8220;$@&#8221; <span>&nbsp;<\/span>\u5219\u53ef\u5f97\u5230 &#8220;p1&#8221; &#8220;p2 p3&#8221; &#8220;p4&#8221; \u8fd9\u4e09\u4e2a\u4e0d\u540c\u7684\u8bcd\u6bb5 <\/li>\n<li> &#8220;$*&#8221; <span>&nbsp;<\/span>\u5219\u53ef\u5f97\u5230 &#8220;p1 p2 p3 p4&#8221; \u8fd9\u4e00\u6574\u4e32\u5355\u4e00\u7684\u8bcd\u6bb5 <\/li>\n<\/ul>\n<h3>10. &amp;&amp; \u4e0e || \u5dee\u5728\u54ea\uff1f<\/h3>\n<ol>\n<li> test\u547d\u4ee4\u6709\u4e24\u79cd\u5f62\u5f0f <\/li>\n<\/ol>\n<ul>\n<li> test expression <\/li>\n<li> [ expression ] <\/li>\n<\/ul>\n<ol>\n<li> bash\u7684test\u76ee\u524d\u652f\u6301\u4e09\u79cd\u6d4b\u8bd5\u5bf9\u8c61 <\/li>\n<\/ol>\n<ul>\n<li> string\uff1a\u5b57\u7b26\u4e32 <\/li>\n<li> integer\uff1a\u6574\u6570 <\/li>\n<li> file\uff1a\u6587\u4ef6 <\/li>\n<\/ul>\n<ol>\n<li> \u5f53expression\u4e3a\u771f\u662f\u8fd4\u56de 0(true) \uff0c\u5426\u5219\u8fd4\u56de \u975e0(false) <\/li>\n<\/ol>\n<ul>\n<li> command1 &amp;&amp; command2 <span>&nbsp;<\/span># command2\u53ea\u6709\u5728command1\u7684RV\u4e3a0(true)\u7684\u6761\u4ef6\u4e0b\u6267\u884c\u3002  <\/li>\n<li> command1 || command2 <span>&nbsp;<\/span># command2\u53ea\u6709\u5728command1\u7684RV\u4e3a\u975e0(false)\u7684\u6761\u4ef6\u4e0b\u6267\u884c\u3002 <\/li>\n<\/ul>\n<ol>\n<li> \u5148\u66ff\u6362\u53d8\u91cf\u518d\u6bd4\u8f83 <\/li>\n<\/ol>\n<pre>A=123\n[&nbsp;-n&nbsp;\"$A\"&nbsp;]&nbsp;&amp;amp;&amp;amp;&nbsp;([&nbsp;\"$A\"&nbsp;-lt&nbsp;100&nbsp;]&nbsp;||&nbsp;echo&nbsp;\"too&nbsp;big\")\nunset&nbsp;A<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>11. &gt; \u4e0e <\/p>\n<ul>\n<li> 0: Standard Input (STDIN) <\/li>\n<li> 1: Standard Output (STDOUT) <\/li>\n<li> 2: Standard Error Output (STDERR) <\/li>\n<\/ul>\n<blockquote style=\"margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light,\">\n<p style=\"padding-top: 8px;padding-bottom: 8px;font-size: 16px;color: black;line-height: 26px\">\u6211\u4eec\u53ef\u7528 \u6211\u4eec\u53ef\u7528&gt;<span>&nbsp;<\/span>\u6765\u6539\u53d8\u9001\u51fa\u7684\u6570\u636e\u4fe1\u9053(stdout, stderr)\uff0c\u4f7f\u4e4b\u8f93\u51fa\u5230\u6307\u5b9a\u7684\u6863\u6848\u3002<\/p>\n<\/blockquote>\n<pre>ls&nbsp;my.file&nbsp;no.such.file&nbsp;1&amp;gt;&nbsp;file.out&nbsp;2&amp;gt;file.err\n#&nbsp;2&amp;gt;&amp;amp;1&nbsp;\u5c31\u662f\u5c06stderr\u5e76\u8fdbstdout\u505a\u8f93\u51fa\nls&nbsp;my.file&nbsp;no.such.file&nbsp;1&amp;gt;&nbsp;file.out&nbsp;2&amp;gt;&amp;amp;1\n#&nbsp;\/dev\/null&nbsp;\u7a7a\nls&nbsp;my.file&nbsp;no.such.file&nbsp;&amp;gt;\/dev\/null&nbsp;2&amp;gt;&amp;amp;1\n\ncat&nbsp;&nbsp;file\n#&nbsp;\u5728&nbsp;IO&nbsp;Redirection&nbsp;\u4e2d\uff0cstdout&nbsp;\u4e0e&nbsp;stderr&nbsp;\u7684\u7ba1\u9053\u4f1a\u5148\u51c6\u5907\u597d\uff0c\u624d\u4f1a\u4ece&nbsp;stdin&nbsp;\u8bfb\u8fdb\u8d44\u6599\u3002&nbsp;\n#&nbsp;\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u4e0a\u4f8b\u4e2d\uff0c&amp;gt;&nbsp;file&nbsp;\u4f1a\u5148\u5c06&nbsp;file&nbsp;\u6e05\u7a7a\uff0c\u7136\u540e\u624d\u8bfb\u8fdb&nbsp;&lt;h3 data-tool=\"mdnice\u7f16\u8f91\u5668\" mp-original-font-size=\"20\" mp-original-line-height=\"32\" style=\"margin-top: 30px;margin-bottom: 15px;outline: 0px;font-weight: bold;font-size: 20px;caret-color: rgb(0, 0, 0);color: rgb(0, 0, 0);font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, \" pingfang sc cambria cochin georgia times new roman serif left normal auto&gt;12. \u4f60\u8981if\u8fd8\u662fcase\u5462\uff1f&lt;\/h3&gt;&lt;pre class=\"brush:php;toolbar:false;\"&gt;#&nbsp;if\necho&nbsp;-n&nbsp;\"Do&nbsp;you&nbsp;want&nbsp;to&nbsp;continue?(Yes\/No):\"\nread&nbsp;YN\nif&nbsp;[&nbsp;\"$YN\"=Y&nbsp;-o&nbsp;\"$YN\"=y&nbsp;-o&nbsp;\"$YN\"=\"Yes\"&nbsp;-o&nbsp;\"$YN\"=\"yes\"&nbsp;-o&nbsp;\"$YN\"=\"YES\"];then\necho&nbsp;\"continue\"\nelse\nexit&nbsp;0\nfi\n\n#&nbsp;case\necho&nbsp;-n&nbsp;\"Do&nbsp;you&nbsp;want&nbsp;to&nbsp;continue?(Yes\/No):\"\nread&nbsp;YN\ncase&nbsp;\"$YN\"&nbsp;in\n[Yy]|[Yy][Ee][Ss])\necho&nbsp;\"continue\"\n;;\n*)\nexit&nbsp;0\nesac<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/h3>\n<h3>13. for what? while\u4e0euntil\u5dee\u5728\u54ea\uff1f<\/h3>\n<pre>#&nbsp;for\nfor&nbsp;((i=1;i<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> break \u662f\u7ed3\u675f loop <\/li>\n<li> return \u662f\u7ed3\u675f function <\/li>\n<li> exit \u662f\u7ed3\u675f script\/shell <\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f60\u5e94\u8be5\u77e5\u9053\u7684Shell \u811a\u672c\u7684\u7ecf\u5178\u5341\u4e09\u95ee\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ecf\u5178\u7684Shell\u5341\u4e09\u95ee 1. \u4e3a\u4f55\u53eb\u505ashell? 2. Shell prompt(PS1)\u4e0eCarriage Return(CR)\u7684\u5173\u7cfb? 3. \u522b\u4ebaecho, \u4f60\u4e5fecho, \u662f\u95eeecho\u77e5\u591a\u5c11? 4. &#8220;&#8221;(\u53cc\u5f15\u53f7)\u4e0e(\u5355\u5f15\u53f7)\u6709\u4ec0\u4e48\u533a\u522b? 5. var=value? export\u524d\u540e\u5dee\u5728\u54ea\uff1f 6. exec\u8ddfsource\u5dee\u5728\u54ea\uff1f 7. ( ) \u4e0e { } \u5dee\u5728\u54ea\uff1f 8.&nbsp; \u4e0e () \u8fd8\u6709 ${} \u5dee\u5728\u54ea\uff1f 9.&nbsp; \u4e0e* \u533a\u522b\u5728\u54ea\uff1f 10. &amp;&amp; \u4e0e || \u5dee\u5728\u54ea\uff1f 11. &gt; \u4e0e 12. \u4f60\u8981if\u8fd8\u662fcase\u5462\uff1f 13. for what? while\u4e0euntil\u5dee\u5728\u54ea\uff1f 1. \u4e3a\u4f55\u53eb\u505ashell? \u6211\u4eec\u77e5\u9053\u8ba1\u7b97\u673a\u7684\u8fd0\u4f5c\u4e0d\u80fd\u79bb\u5f00\u786c\u4ef6\uff0c\u4f46\u4f7f\u7528\u8005\u5374\u65e0\u6cd5\u76f4\u63a5\u64cd\u4f5c\u786c\u4ef6\uff0c\u786c\u4ef6\u7684\u9a71\u52a8\u53ea\u80fd\u901a\u8fc7\u4e00\u79cd\u79f0\u4e3a\u64cd\u4f5c\u7cfb\u7edf(OS\uff0cOpertating System)\u7684\u8f6f\u4ef6\u6765\u7ba1\u63a7\u3002linux\u4e25\u683c\u6765\u8bf4\u53ea\u662f\u4e00\u4e2a\u64cd\u4f5c\u7cfb\u7edf(OS)\uff0c\u6211\u4eec\u79f0\u4e4b\u4e3a\u5185\u6838(kernel)\u3002 \u4f7f\u7528\u8005\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u64cd\u4f5c\u4e00\u4e2akernel\uff0c\u800c\u662f\u901a\u8fc7kernel\u7684\u5916\u58f3\u7a0b\u5e8f\uff0c\u4e5f\u5c31\u662f\u6240\u8c13\u7684shell\uff0c\u6765\u4e0ekernel\u6c9f\u901a\u3002shell\u662f\u4e00\u4e2a\u4f7f\u7528\u8005\u4e0e\u7cfb\u7edf\u7684\u4ea4\u4e92\u754c\u9762(interface), \u53ea\u80fd\u8ba9\u4f7f\u7528\u8005\u901a\u8fc7\u547d\u4ee4\u884c(command line)\u6765\u4f7f\u7528\u7cfb\u7edf\u6765\u5b8c\u6210\u5de5\u4f5c\u3002\u56e0\u6b64 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-16613","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=16613"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16613\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=16613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=16613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=16613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}