{"id":16617,"date":"2024-11-18T08:50:17","date_gmt":"2024-11-18T00:50:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/16617\/"},"modified":"2024-11-18T08:50:17","modified_gmt":"2024-11-18T00:50:17","slug":"%e5%85%b3%e4%ba%8e-shell-%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\/16617\/","title":{"rendered":"\u5173\u4e8e Shell \u811a\u672c\u7684\u7ecf\u5178\u5341\u4e09\u95ee"},"content":{"rendered":"<p> <\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/275\/013\/f09bd921b6e696eede596bf84e221b6e-0.png\" class=\"aligncenter\" title=\"\u5173\u4e8e Shell \u811a\u672c\u7684\u7ecf\u5178\u5341\u4e09\u95ee\u63d2\u56fe\" alt=\"\u5173\u4e8e Shell \u811a\u672c\u7684\u7ecf\u5178\u5341\u4e09\u95ee\u63d2\u56fe\" \/><\/p>\n<h4><span style=\"font-size: 19.2px;color: inherit;line-height: 30px\">1\u3001\u4e3a\u4f55\u53eb\u505a Shell\uff1f<\/span><\/h4>\n<p style=\"margin-top: 1.5em;margin-bottom: 1.5em;color: inherit;font-family:\">\u6211\u4eec\u77e5\u9053\u8ba1\u7b97\u673a\u7684\u8fd0\u4f5c\u79bb\u4e0d\u5f00\u786c\u4ef6\uff0c\u4f46\u5374\u65e0\u6cd5\u76f4\u63a5\u64cd\u4f5c\u786c\u4ef6\uff0c\u786c\u4ef6\u7684\u9a71\u52a8\u53ea\u80fd\u901a\u8fc7\u4e00\u79cd\u79f0\u4e4b\u4e3a\u201c\u64cd\u4f5c\u7cfb\u7edf\uff08OS\uff0cOpertating System\uff09\u7684\u8f6f\u4ef6\u6765\u7ba1\u63a7\u3002Linux \u4e25\u683c\u6765\u8bf4\u662f\u4e00\u4e2a\u64cd\u4f5c\u7cfb\u7edf\uff08OS\uff09\u3002<\/p>\n<p style=\"margin-top: 1.5em;margin-bottom: 1.5em;color: inherit;font-family:\">\u4f7f\u7528\u8005\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u64cd\u4f5c Kernel\uff0c\u800c\u662f\u901a\u8fc7 Kernel \u7684\u201c\u5916\u58f3\u201d\u7a0b\u5e8f\uff0c\u4e5f\u5c31\u662f\u6240\u8c13\u7684 Shell \u6765\u4e0e Kernel \u6c9f\u901a\u3002Shell \u662f\u4e00\u4e2a\u4f7f\u7528\u8005\u4e0e\u7cfb\u7edf\u7684\u4ea4\u4e92\u754c\u9762\uff08Interface\uff09\uff0c\u53ea\u80fd\u901a\u8fc7\u547d\u4ee4\u884c\uff08Command line\uff09\u6765\u4f7f\u7528\u7cfb\u7edf\u6765\u5b8c\u6210\u5de5\u4f5c\u3002\u56e0\u6b64 Shell \u6700\u7b80\u5355\u7684\u5b9a\u4e49\u5c31\u662f\uff1a\u547d\u4ee4\u89e3\u8bd1\u5668\uff08Command Interpreter\uff09<\/p>\n<ul>\n<li>\n<p style=\"line-height: 25px\"><span style=\"color: inherit;line-height: 25px\">\u5c06\u4f7f\u7528\u8005\u7684\u547d\u4ee4\u7ffb\u8bd1\u7ed9\u5185\u6838\u5904\u7406\uff1b<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\"><span style=\"color: inherit;line-height: 25px\">\u540c\u65f6\uff0c\u5c06\u5185\u6838\u5904\u7406\u7ed3\u679c\u7ffb\u8bd1\u7ed9\u4f7f\u7528\u8005\u3002<\/span><\/p>\n<\/li>\n<\/ul>\n<p style=\"margin-top: 1.5em;margin-bottom: 1.5em;color: inherit;font-family:\">\u4e0d\u540c\u7684 OS \u4f7f\u7528\u4e0d\u540c\u7684 Kernel\uff1b\u540c\u4e00\u4e2a kernel \u4e4b\u4e0a\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684 Shell\uff0c\u5e38\u89c1\u7684 Shell \u6709sh\u3001bash\u3001csh\u3001ksh \u7b49<\/p>\n<h4><span style=\"font-size: 19.2px;color: inherit;line-height: 30px\">2\u3001Shell prompt\uff08PS1\uff09\u4e0e Carriage Return\uff08CR\uff09\u7684\u5173\u7cfb\uff1f<\/span><\/h4>\n<p style=\"margin-top: 1.5em;margin-bottom: 1.5em;color: inherit;font-family:\">\u6210\u529f\u767b\u5f55\u4e00\u4e2a Shell \u7ec8\u7aef\u540e\uff0c\u5149\u6807\u5de6\u8fb9\u90e8\u5206\u79f0\u4e4b\u4e3a\u63d0\u793a\u7b26 Prompt\uff0c\u901a\u5e38\u4e00\u822c\u7528\u6237\u4f7f\u7528$\uff0c\u7ba1\u7406\u5458\u7528\u6237\u4f7f\u7528#<\/p>\n<ul>\n<li>\n<p style=\"line-height: 25px\"><span style=\"color: inherit;line-height: 25px\">Shell Prompt\uff1a\u53ef\u4ee5\u8f93\u5165\u547d\u4ee4\u4e86\uff0c\u952e\u5165\u547d\u4ee4\u540e\uff0c\u76f4\u5230\u8bfb\u8fdb CR\uff08Carriage Return\uff09\u5b57\u7b26\u4e3a\u6b62<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\"><span style=\"color: inherit;line-height: 25px\">Carriage Return\uff1a\u53ef\u4ee5\u6267\u884c\u547d\u4ee4\u4e86<\/span><\/p>\n<\/li>\n<\/ul>\n<blockquote style=\"padding: 15px 15px 15px 1rem;border-left-width: 6px;font-size: 14.4px;font-family:\">\n<p style=\"font-size: 14.4px;color: inherit;line-height: 23px\">\u82e5\u4ece\u6280\u672f\u7684\u7ec6\u8282\u6765\u770b\uff0cShell \u4f1a\u4f9d\u636e IFS\uff08Internal Field Seperator\uff09\u5c06 Command line \u6240\u8f93\u5165\u7684\u6587\u5b57\u62c6\u89e3\u4e3a\u201d\u5b57\u6bb5\u201d\uff08word\/field\uff09\u3002\u7136\u540e\u518d\u9488\u5bf9\u7279\u6b8a\u5b57\u7b26\uff08meta\uff09\u5148\u4f5c\u5904\u7406\uff0c\u6700\u540e\u91cd\u7ec4\u6574\u884ccommand line\u3002<\/p>\n<\/blockquote>\n<h4><span style=\"font-size: 19.2px;color: inherit;line-height: 30px\">3\u3001\u522b\u4ebaecho\uff0c\u4f60\u4e5fecho\uff0c\u8bd5\u95ee echo \u77e5\u591a\u5c11?<\/span><\/h4>\n<p style=\"margin-top: 1.5em;margin-bottom: 1.5em;color: inherit;font-family:\">echo \u5c06 argument\u9001\u5230\u6807\u51c6\u8f93\u51fa\uff08stdout\uff09\uff0c\u901a\u5e38\u663e\u793a\u5728\u5c4f\u5e55<\/p>\n<blockquote style=\"padding: 15px 15px 15px 1rem;border-left-width: 6px;font-size: 14.4px;font-family:\">\n<p style=\"font-size: 14.4px;color: inherit;line-height: 23px\">stdin \u6807\u51c6\u8f93\u5165<br \/>stdout \u6807\u51c6\u8f93\u51fa<br \/>stderr \u6807\u51c6\u9519\u8bef\u8f93\u51fa<\/p>\n<\/blockquote>\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<h4><span style=\"font-size: 19.2px;color: inherit;line-height: 30px\">4\u3001\u53cc\u5f15\u53f7&#8221;&#8221;\u4e0e\u5355\u5f15\u53f7&#8221;\u6709\u4ec0\u4e48\u533a\u522b?<\/span><\/h4>\n<ul>\n<li>\n<p style=\"line-height: 25px\">hard quote\uff1a&#8221;\uff08\u5355\u5f15\u53f7\uff09\uff0c\u5173\u95ed\u6240\u6709\u5f15\u7528<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">soft quote\uff1a&#8221;&#8221;\uff08\u53cc\u5f15\u53f7\uff09\uff0c\u4fdd\u7559$\u5f15\u7528<\/p>\n<\/li>\n<\/ul>\n<h4><span style=\"font-size: 19.2px;color: inherit;line-height: 30px\">5\u3001var=value? export \u524d\u540e\u5dee\u5728\u54ea\uff1f<\/span><\/h4>\n<ul>\n<li>\n<p style=\"line-height: 25px\">\u53d8\u91cf\u5b9a\u4e49\uff1aname=value\uff0c\u7b49\u53f7\u5de6\u53f3\u4e24\u8fb9\u4e0d\u80fd\u4f7f\u7528\u5206\u9694\u7b26\u3002<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">\u53d8\u91cf\u66ff\u6362\uff1aecho ${name}<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">export\u53d8\u91cf\uff1aexport name=value\uff0c\u4f7f\u53d8\u91cf\u6210\u4e3a\u73af\u5883\u53d8\u91cf<\/p>\n<\/li>\n<\/ul>\n<pre>#&nbsp;\u672c\u5730\u53d8\u91cf\nA=B\n#&nbsp;\u53d6\u6d88\u53d8\u91cf\nunset&nbsp;A\n#&nbsp;\u73af\u5883\u53d8\u91cfexport&nbsp;A=B<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4><span style=\"font-size: 19.2px;color: inherit;line-height: 30px\">6\u3001exec \u8ddf source \u5dee\u5728\u54ea\uff1f<\/span><\/h4>\n<blockquote style=\"padding: 15px 15px 15px 1rem;border-left-width: 6px;font-size: 14.4px;font-family:\">\n<p style=\"font-size: 14.4px;color: inherit;line-height: 23px\">\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<\/p>\n<\/blockquote>\n<pre>#&nbsp;\u521b\u5efa\u5b50shell\u6267\u884c\u811a\u672c\n.\/1.sh\n#&nbsp;\u5f53\u524dshell\u6267\u884c\nsource&nbsp;1.sh\n#&nbsp;\u5f53\u524dshell\u6267\u884c\u540e\u9000\u51fa\nexec&nbsp;1.sh<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4><span style=\"font-size: 19.2px;color: inherit;line-height: 30px\">7\u3001( ) \u4e0e { } \u5dee\u5728\u54ea\uff1f<\/span><\/h4>\n<p style=\"margin-top: 1.5em;margin-bottom: 1.5em;color: inherit;font-family:\">( )\u5c06 command group \u7f6e\u4e8e sub-shell \u6267\u884c<br \/>{ }\u5219\u662f\u5728\u540c\u4e00\u4e2ashell\u5185\u5b8c\u6210<\/p>\n<h4><span style=\"font-size: 19.2px;color: inherit;line-height: 30px\">8\u3001$(()) \u4e0e $() \u8fd8\u6709 ${} \u5dee\u5728\u54ea\uff1f<\/span><\/h4>\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\n#&nbsp;1.&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#&nbsp;2.&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;3.&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;4.&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<hr>\n<pre>#&nbsp;5.&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\n#&nbsp;6.&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\n#&nbsp;7.&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(\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(\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(\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(\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(\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(\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<hr>\n<pre>#&nbsp;8.&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\n#&nbsp;9.&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\n#&nbsp;10.$((&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<h4><span style=\"font-size: 19.2px;color: inherit;line-height: 30px\">9\u3001$@ \u4e0e $* \u533a\u522b\u5728\u54ea\uff1f<\/span><\/h4>\n<ul>\n<li>\n<p style=\"line-height: 25px\">&#8220;$@&#8221;\u5219\u53ef\u5f97\u5230 \u201cp1\u201d \u201cp2 p3\u201d \u201cp4\u201d \u8fd9\u4e09\u4e2a\u4e0d\u540c\u7684\u8bcd\u6bb5<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">&#8220;$*&#8221;\u5219\u53ef\u5f97\u5230 \u201cp1 p2 p3 p4\u201d \u8fd9\u4e00\u6574\u4e32\u5355\u4e00\u7684\u8bcd\u6bb5<\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\">\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<span style=\"line-height: 0px\">\u200d<\/span><\/p>\n<\/li>\n<\/ul>\n<h4><span style=\"font-size: 19.2px;color: inherit;line-height: 30px\">10\u3001&amp;&amp; \u4e0e || \u5dee\u5728\u54ea\uff1f<\/span><\/h4>\n<p style=\"margin-top: 1.5em;margin-bottom: 1.5em;color: inherit;font-family:\">1\u3001test\u547d\u4ee4\u6709\u4e24\u79cd\u5f62\u5f0f<\/p>\n<ul>\n<li>\n<p style=\"line-height: 25px\"><span style=\"color: inherit;line-height: 25px\">test expression<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\"><span style=\"color: inherit;line-height: 25px\">[ expression ]<\/span><\/p>\n<\/li>\n<\/ul>\n<p style=\"margin-top: 1.5em;margin-bottom: 1.5em;color: inherit;font-family:\">2\u3001bash\u7684test\u76ee\u524d\u652f\u6301\u4e09\u79cd\u6d4b\u8bd5\u5bf9\u8c61<\/p>\n<ul>\n<li>\n<p style=\"line-height: 25px\"><span style=\"color: inherit;line-height: 25px\">string\uff1a\u5b57\u7b26\u4e32<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\"><span style=\"color: inherit;line-height: 25px\">integer\uff1a\u6574\u6570<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\"><span style=\"color: inherit;line-height: 25px\">file\uff1a\u6587\u4ef6<\/span><\/p>\n<\/li>\n<\/ul>\n<p style=\"margin-top: 1.5em;margin-bottom: 1.5em;color: inherit;font-family:\">3\u3001\u5f53 expression \u4e3a\u771f\u662f\u8fd4\u56de 0\uff08true\uff09 \uff0c\u5426\u5219\u8fd4\u56de \u975e0\uff08false\uff09<\/p>\n<ul>\n<li>\n<p style=\"line-height: 25px\"><span style=\"color: inherit;line-height: 25px\">command1 &amp;&amp; command2 &nbsp;command2 \u53ea\u6709\u5728 command1 \u7684RV\u4e3a0\uff08True\uff09\u7684\u6761\u4ef6\u4e0b\u6267\u884c\u3002<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\"><span style=\"color: inherit;line-height: 25px\">command1 || command2 &nbsp;command2\u53ea\u6709\u5728command1\u7684RV\u4e3a\u975e0(False)\u7684\u6761\u4ef6\u4e0b\u6267\u884c\u3002<\/span><\/p>\n<\/li>\n<\/ul>\n<p style=\"margin-top: 1.5em;margin-bottom: 1.5em;color: inherit;font-family:\">4\u3001\u5148\u66ff\u6362\u53d8\u91cf\u518d\u6bd4\u8f83<\/p>\n<pre>A=123[&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\")unset&nbsp;A<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4><span style=\"font-size: 19.2px;color: inherit;line-height: 30px\">11\u3001&gt; \u4e0e <\/span><\/h4>\n<blockquote style=\"padding: 15px 15px 15px 1rem;border-left-width: 6px;font-size: 14.4px;font-family:\">\n<p style=\"font-size: 14.4px;color: inherit;line-height: 23px\">0: Standard Input\uff08STDIN\uff09<br \/>1: Standard Output \uff08STDOUT\uff09<br \/>2: Standard Error Output\uff08STDERR\uff09<\/p>\n<\/blockquote>\n<p style=\"margin-top: 1.5em;margin-bottom: 1.5em;color: inherit;font-family:\">\u6211\u4eec\u53ef\u7528 <\/p>\n<p style=\"margin-top: 1.5em;margin-bottom: 1.5em;color: inherit;font-family:\">\u6211\u4eec\u53ef\u7528 &gt; \u6765\u6539\u53d8\u9001\u51fa\u7684\u6570\u636e\u4fe1\u9053\uff08stdout, stderr\uff09\uff0c\u4f7f\u4e4b\u8f93\u51fa\u5230\u6307\u5b9a\u7684\u6863\u6848\u3002<\/p>\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\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;h4 mp-original-font-size=\"19.200000762939453\" mp-original-line-height=\"30\" style=\"margin-top: 1.5em;margin-bottom: 1.5em;outline: 0px;font-weight: bold;font-size: 19.2px;caret-color: rgb(62, 62, 62);color: inherit;font-family: \" helvetica neue sans gb yahei arial sans-serif normal auto&gt;&lt;span mp-original-font-size=\"19.200000762939453\" mp-original-line-height=\"30\" style=\"outline: 0px;font-size: 19.2px;color: inherit;line-height: 30px;\"&gt;12\u3001\u4f60\u8981if\u8fd8\u662fcase\u5462\uff1f&lt;\/span&gt;&lt;\/h4&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 <\/p>\n<h4><span style=\"font-size: 19.2px;color: inherit;line-height: 30px\">13\u3001for what? while\u4e0euntil\u5dee\u5728\u54ea\uff1f<\/span><\/h4>\n<pre>#&nbsp;for\nfor&nbsp;((i=1;i<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\n<p style=\"line-height: 25px\"><span style=\"color: inherit;line-height: 25px\">break \u662f\u7ed3\u675f loop<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\"><span style=\"color: inherit;line-height: 25px\">return \u662f\u7ed3\u675f function<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 25px\"><span style=\"color: inherit;line-height: 25px\">exit \u662f\u7ed3\u675f script\/shell<\/span><\/p>\n<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5173\u4e8e Shell \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>1\u3001\u4e3a\u4f55\u53eb\u505a Shell\uff1f \u6211\u4eec\u77e5\u9053\u8ba1\u7b97\u673a\u7684\u8fd0\u4f5c\u79bb\u4e0d\u5f00\u786c\u4ef6\uff0c\u4f46\u5374\u65e0\u6cd5\u76f4\u63a5\u64cd\u4f5c\u786c\u4ef6\uff0c\u786c\u4ef6\u7684\u9a71\u52a8\u53ea\u80fd\u901a\u8fc7\u4e00\u79cd\u79f0\u4e4b\u4e3a\u201c\u64cd\u4f5c\u7cfb\u7edf\uff08OS\uff0cOpertating System\uff09\u7684\u8f6f\u4ef6\u6765\u7ba1\u63a7\u3002Linux \u4e25\u683c\u6765\u8bf4\u662f\u4e00\u4e2a\u64cd\u4f5c\u7cfb\u7edf\uff08OS\uff09\u3002 \u4f7f\u7528\u8005\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u64cd\u4f5c Kernel\uff0c\u800c\u662f\u901a\u8fc7 Kernel \u7684\u201c\u5916\u58f3\u201d\u7a0b\u5e8f\uff0c\u4e5f\u5c31\u662f\u6240\u8c13\u7684 Shell \u6765\u4e0e Kernel \u6c9f\u901a\u3002Shell \u662f\u4e00\u4e2a\u4f7f\u7528\u8005\u4e0e\u7cfb\u7edf\u7684\u4ea4\u4e92\u754c\u9762\uff08Interface\uff09\uff0c\u53ea\u80fd\u901a\u8fc7\u547d\u4ee4\u884c\uff08Command line\uff09\u6765\u4f7f\u7528\u7cfb\u7edf\u6765\u5b8c\u6210\u5de5\u4f5c\u3002\u56e0\u6b64 Shell \u6700\u7b80\u5355\u7684\u5b9a\u4e49\u5c31\u662f\uff1a\u547d\u4ee4\u89e3\u8bd1\u5668\uff08Command Interpreter\uff09 \u5c06\u4f7f\u7528\u8005\u7684\u547d\u4ee4\u7ffb\u8bd1\u7ed9\u5185\u6838\u5904\u7406\uff1b \u540c\u65f6\uff0c\u5c06\u5185\u6838\u5904\u7406\u7ed3\u679c\u7ffb\u8bd1\u7ed9\u4f7f\u7528\u8005\u3002 \u4e0d\u540c\u7684 OS \u4f7f\u7528\u4e0d\u540c\u7684 Kernel\uff1b\u540c\u4e00\u4e2a kernel \u4e4b\u4e0a\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684 Shell\uff0c\u5e38\u89c1\u7684 Shell \u6709sh\u3001bash\u3001csh\u3001ksh \u7b49 2\u3001Shell prompt\uff08PS1\uff09\u4e0e Carriage Return\uff08CR\uff09\u7684\u5173\u7cfb\uff1f \u6210\u529f\u767b\u5f55\u4e00\u4e2a Shell \u7ec8\u7aef\u540e\uff0c\u5149\u6807\u5de6\u8fb9\u90e8\u5206\u79f0\u4e4b\u4e3a\u63d0\u793a\u7b26 Prompt\uff0c\u901a\u5e38\u4e00\u822c\u7528\u6237\u4f7f\u7528$\uff0c\u7ba1\u7406\u5458\u7528\u6237\u4f7f\u7528# Shell Prompt\uff1a\u53ef\u4ee5\u8f93\u5165\u547d\u4ee4\u4e86\uff0c\u952e\u5165\u547d\u4ee4\u540e\uff0c\u76f4\u5230\u8bfb\u8fdb CR\uff08Carriage Return\uff09\u5b57\u7b26\u4e3a\u6b62 Carriage Return\uff1a\u53ef\u4ee5\u6267\u884c\u547d\u4ee4\u4e86 \u82e5\u4ece\u6280\u672f\u7684\u7ec6\u8282\u6765\u770b\uff0cShell \u4f1a\u4f9d\u636e IFS\uff08Internal Field Seperator\uff09\u5c06 Command line \u6240\u8f93\u5165\u7684\u6587\u5b57\u62c6\u89e3\u4e3a\u201d\u5b57\u6bb5\u201d\uff08word\/field\uff09\u3002\u7136\u540e\u518d\u9488\u5bf9\u7279\u6b8a\u5b57\u7b26\uff08meta\uff09\u5148\u4f5c\u5904\u7406\uff0c\u6700\u540e\u91cd\u7ec4\u6574\u884ccommand line\u3002 3\u3001\u522b\u4ebaecho\uff0c\u4f60\u4e5fecho\uff0c\u8bd5\u95ee echo [&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-16617","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16617","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=16617"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16617\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=16617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=16617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=16617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}