<div class="rich_media_content js_underline_content
 " id="js_content"> <section data-mpa-powered-by="yiban.io" style="white-space: normal;text-align: center;"><br></section><section style='margin-bottom: 20px;white-space: normal;caret-color: rgb(62, 62, 62);font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;text-size-adjust: auto;color: rgb(62, 62, 62);font-size: 16px;letter-spacing: 1px;word-spacing: 1px;text-align: left;'><p id="_img_parent_tmp" style="text-align:center"><strong style='color: rgb(0, 0, 0);word-spacing: 0.1em;caret-color: rgb(51, 51, 51);font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);font-size: 18px;outline: 0px;'><span style='outline: 0px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;'><strong style='font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;'><span style="letter-spacing: 1px;"><img src="https://img.php.cn/upload/article/001/275/013/5e9e2e09cb66ac70555a8b09900a7da1-0.png" alt="你应该知道的Shell 脚本的经典十三问" ></span></strong></span></strong></p></section><ul class="list-paddingleft-1" mp-original-font-size="16" mp-original-line- style="max-width:90%" style='margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;visibility: visible;line-height: 25px;'> <li style="outline: 0px;visibility: visible;line-height: 25px;"><p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;">经典的Shell十三问</p></li> <ul class="list-paddingleft-1" mp-original-font-size="16" mp-original-line-height="25" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;list-style-type: square;visibility: visible;line-height: 25px;"> <li style="outline: 0px;visibility: visible;line-height: 25px;"><p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;">1. 为何叫做shell?</p></li> <li style="outline: 0px;visibility: visible;line-height: 25px;"><p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;">2. Shell prompt(PS1)与Carriage Return(CR)的关系?</p></li> <li style="outline: 0px;visibility: visible;line-height: 25px;"><p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;">3. 别人echo, 你也echo, 是问echo知多少?</p></li> <li style="outline: 0px;visibility: visible;line-height: 25px;"><p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;">4. ""(双引号)与(单引号)有什么区别?</p></li> <li style="outline: 0px;visibility: visible;line-height: 25px;"><p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;">5. var=value? export前后差在哪?</p></li> <li style="outline: 0px;visibility: visible;line-height: 25px;"><p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;">6. exec跟source差在哪?</p></li> <li style="outline: 0px;visibility: visible;line-height: 25px;"><p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;">7. ( ) 与 { } 差在哪?</p></li> <li style="outline: 0px;visibility: visible;line-height: 25px;"><p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;">8.<span> </span><span mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;cursor: pointer;visibility: visible;line-height: 25px;"><span data-formula="(()) 与 " data-formula-type="inline-equation" mp-original-font-size="16" mp-original-line-height="25" role="presentation" style="outline: 0px;visibility: visible;line-height: 25px;"><svg aria-hidden="true" focusable="false" mp-original-font-size="16" mp-original-line-height="25" role="img" style="vertical-align: -0.566ex;width: 6.259ex;height: 2.262ex;visibility: visible;line-height: 25px;" viewbox="0 -750 2766.3 1000" xmlns="http://www.w3.org/2000/svg"><g fill="currentColor" mp-original-font-size="16" mp-original-line-height="25" stroke="currentColor" stroke-width="0" style="visibility: visible;line-height: 25px;" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math" mp-original-font-size="16" mp-original-line-height="25" style="visibility: visible;line-height: 25px;"><g data-mml-node="mo" mp-original-font-size="16" mp-original-line-height="25" style="visibility: visible;line-height: 25px;"><path d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z" data-c="28" mp-original-font-size="16" mp-original-line-height="25" style="visibility: visible;line-height: 25px;"></path></g><g data-mml-node="mo" mp-original-font-size="16" mp-original-line-height="25" style="visibility: visible;line-height: 25px;" transform="translate(389, 0)"><path d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z" data-c="28" mp-original-font-size="16" mp-original-line-height="25" style="visibility: visible;line-height: 25px;"></path></g><g data-mml-node="mo" mp-original-font-size="16" mp-original-line-height="25" style="visibility: visible;line-height: 25px;" transform="translate(778, 0)"><path d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z" data-c="29" mp-original-font-size="16" mp-original-line-height="25" style="visibility: visible;line-height: 25px;"></path></g><g data-mml-node="mo" mp-original-font-size="16" mp-original-line-height="25" style="visibility: visible;line-height: 25px;" transform="translate(1167, 0)"><path d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z" data-c="29" mp-original-font-size="16" mp-original-line-height="25" style="visibility: visible;line-height: 25px;"></path></g><g data-mml-node="mo" mp-original-font-size="16" mp-original-line-height="25" style="visibility: visible;line-height: 25px;" transform="translate(1833.8, 0)"><text data-variant="normal" font-family="serif" font-size="932.6px" mp-original-font-size="932.5999755859375" mp-original-line-height="1492" style="visibility: visible;font-size: 932.5999755859375px;line-height: 1492px;" transform="matrix(1 0 0 -1 0 0)">与</text></g></g></g></svg></span></span>() 还有 ${} 差在哪?</p></li> <li style="outline: 0px;visibility: visible;line-height: 25px;"><p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;">9.<span> </span><span mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;cursor: pointer;visibility: visible;line-height: 25px;"><svg aria-hidden="true" focusable="false" mp-original-font-size="16" mp-original-line-height="25" role="img" style="vertical-align: -0.452ex;width: 4.499ex;height: 2.149ex;visibility: visible;line-height: 25px;" viewbox="0 -750 1988.3 950" xmlns="http://www.w3.org/2000/svg"><g fill="currentColor" mp-original-font-size="16" mp-original-line-height="25" stroke="currentColor" stroke-width="0" style="visibility: visible;line-height: 25px;" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math" mp-original-font-size="16" mp-original-line-height="25" style="visibility: visible;line-height: 25px;"><g data-mjx-texclass="ORD" data-mml-node="TeXAtom" mp-original-font-size="16" mp-original-line-height="25" style="visibility: visible;line-height: 25px;"><g data-mml-node="mo" mp-original-font-size="16" mp-original-line-height="25" style="visibility: visible;line-height: 25px;"><path d="M56 347Q56 429 86 498T164 612T270 680T386 705Q522 705 622 603T722 349Q722 126 608 126Q541 126 513 176Q512 177 512 179T510 182L509 183Q508 183 503 177T487 163T464 146T429 132T385 126Q311 126 251 186T190 347Q190 448 251 508T385 568Q426 568 460 548T509 511T531 479H555Q580 479 582 478Q586 477 587 468Q588 454 588 338V260Q588 200 593 182T619 163Q641 163 655 178T674 223T680 273T682 325V330Q682 426 647 500Q611 569 544 618T388 668Q271 668 184 577T96 347Q96 216 180 121T396 26Q421 26 446 28T493 34T535 43T573 52T605 63T629 72T647 80T657 84H716Q722 78 722 74Q722 65 675 45T547 7T392 -11Q255 -11 156 90T56 347ZM274 347Q274 266 308 214T390 162Q420 162 449 182T498 235L504 245V449L498 459Q453 532 387 532Q347 532 311 483T274 347Z" data-c="40" mp-original-font-size="16" mp-original-line-height="25" style="visibility: visible;line-height: 25px;"></path></g></g></g></g></svg>与</span>* 区别在哪?</p></li> <li style="outline: 0px;visibility: visible;line-height: 25px;"><p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;">10. && 与 || 差在哪?</p></li> <li style="outline: 0px;visibility: visible;line-height: 25px;"><p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;">11. > 与 < 差在哪?</p></li> <li style="outline: 0px;visibility: visible;line-height: 25px;"><p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;">12. 你要if还是case呢?</p></li> <li style="outline: 0px;visibility: visible;line-height: 25px;"><p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;">13. for what? while与until差在哪?</p></li> </ul> </ul> <h3 data-tool="mdnice编辑器" 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, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;visibility: visible;line-height: 32px;'>1. 为何叫做shell?</h3> <blockquote data-tool="mdnice编辑器" mp-original-font-size="14.399999618530273" mp-original-line-height="23" style='margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;outline: 0px;border-left-color: rgba(0, 0, 0, 0.4);color: rgb(106, 115, 125);font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;overflow: auto;background-color: rgba(0, 0, 0, 0.047);visibility: visible;line-height: 23px;'><p mp-original-font-size="16" mp-original-line-height="26" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;font-size: 16px;color: black;line-height: 26px;visibility: visible;">我们知道计算机的运作不能离开硬件,但使用者却无法直接操作硬件,硬件的驱动只能通过一种称为<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;visibility: visible;line-height: 26px;'>操作系统</code>(<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;visibility: visible;line-height: 26px;'>OS</code>,<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;visibility: visible;line-height: 26px;'>Opertating System</code>)的软件来管控。<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;visibility: visible;line-height: 26px;'>linux</code>严格来说只是一个<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;visibility: visible;line-height: 26px;'>操作系统</code>(<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;visibility: visible;line-height: 26px;'>OS</code>),我们称之为<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;visibility: visible;line-height: 26px;'>内核</code>(<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;visibility: visible;line-height: 26px;'>kernel</code>)。</p></blockquote> <blockquote data-tool="mdnice编辑器" mp-original-font-size="14.399999618530273" mp-original-line-height="23" style='margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;outline: 0px;border-left-color: rgba(0, 0, 0, 0.4);color: rgb(106, 115, 125);font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;overflow: auto;background-color: rgba(0, 0, 0, 0.047);line-height: 23px;'><p mp-original-font-size="16" mp-original-line-height="26" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;font-size: 16px;color: black;line-height: 26px;">使用者没有办法直接操作一个<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>kernel</code>,而是通过<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>kernel</code>的外壳程序,也就是所谓的<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>shell</code>,来与<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>kernel</code>沟通。<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>shell</code>是一个使用者与系统的<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>交互界面(interface)</code>, 只能让使用者通过<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>命令行</code>(<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>command line</code>)来使用系统来完成工作。因此 ,<br mp-original-font-size="16" mp-original-line-height="26" style="outline: 0px;line-height: 26px;">shell最简单的定义就是——命令解译器(Command Interpreter):</p></blockquote> <ul class="list-paddingleft-1" data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="25" style='margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 25px;'> <li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">将使用者的命令翻译给核心处理;</section></li> <li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">同时,将核心处理结果翻译给使用者。</section></li> </ul> <blockquote data-tool="mdnice编辑器" mp-original-font-size="14.399999618530273" mp-original-line-height="23" style='margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;outline: 0px;border-left-color: rgba(0, 0, 0, 0.4);color: rgb(106, 115, 125);font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;overflow: auto;background-color: rgba(0, 0, 0, 0.047);line-height: 23px;'><p mp-original-font-size="16" mp-original-line-height="26" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;font-size: 16px;color: black;line-height: 26px;">不同的<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>OS</code>使用不同的<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>kernel</code>;<br mp-original-font-size="16" mp-original-line-height="26" style="outline: 0px;line-height: 26px;">同一个<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>kernel</code>之上,也可以使用不同的<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>shell</code><br mp-original-font-size="16" mp-original-line-height="26" style="outline: 0px;line-height: 26px;">常见的<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>shell</code>有<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>sh; bash; csh; ksh;</code>等</p></blockquote> <h3 data-tool="mdnice编辑器" 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, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 32px;'>2. Shell prompt(PS1)与Carriage Return(CR)的关系?</h3> <blockquote data-tool="mdnice编辑器" mp-original-font-size="14.399999618530273" mp-original-line-height="23" style='margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;outline: 0px;border-left-color: rgba(0, 0, 0, 0.4);color: rgb(106, 115, 125);font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;overflow: auto;background-color: rgba(0, 0, 0, 0.047);line-height: 23px;'><p mp-original-font-size="16" mp-original-line-height="26" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;font-size: 16px;color: black;line-height: 26px;">成功登录一个shell终端后,游标<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>cursor</code>左边部分,称之为提示符<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>prompt</code><br mp-original-font-size="16" mp-original-line-height="26" style="outline: 0px;line-height: 26px;">通常一般用户使用<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>$</code>,管理员用户<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>root</code>使用<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>#</code></p></blockquote> <ul class="list-paddingleft-1" data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="25" style='margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 25px;'> <li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">shell prompt:可以输入命令了<br mp-original-font-size="16" mp-original-line-height="26" style="outline: 0px;line-height: 26px;">键入命令后,直到读进<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: " operator mono consolas monaco menlo monospace break-all>CR(Carriage Return)</code>字符为止</section></li> <code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>CR(Carriage Return)</code>字符为止<li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">Carriage Return:可以执行命令了</section></li> </ul> <blockquote data-tool="mdnice编辑器" mp-original-font-size="14.399999618530273" mp-original-line-height="23" style='margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;outline: 0px;border-left-color: rgba(0, 0, 0, 0.4);color: rgb(106, 115, 125);font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;overflow: auto;background-color: rgba(0, 0, 0, 0.047);line-height: 23px;'><p mp-original-font-size="16" mp-original-line-height="26" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;font-size: 16px;color: black;line-height: 26px;">若从技术的细节来看,shell会依据IFS(Internal Field Seperator) 将command line所输入的文字拆解为"字段"(word/field)。然后再针对特殊字符(meta)先作处理,最后重组整行command line。</p></blockquote> <h3 data-tool="mdnice编辑器" 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, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 32px;'>3. 别人echo, 你也echo, 是问echo知多少?</h3> <blockquote data-tool="mdnice编辑器" mp-original-font-size="14.399999618530273" mp-original-line-height="23" style='margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;outline: 0px;border-left-color: rgba(0, 0, 0, 0.4);color: rgb(106, 115, 125);font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;overflow: auto;background-color: rgba(0, 0, 0, 0.047);line-height: 23px;'> <p mp-original-font-size="16" mp-original-line-height="26" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;font-size: 16px;color: black;line-height: 26px;"><code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>echo</code>将<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>argument</code>送到<code mp-original-font-size="14" mp-original-line-height="26" style='margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;'>标准输出(stdout)</code></p>Carriage Return:可以执行命令了<blockquote data-tool="mdnice编辑器" mp-original-font-size="14.399999618530273" mp-original-line-height="23" style="margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;outline: 0px;border-left-color: rgba(0, 0, 0, 0.4);color: rgb(106, 115, 125);font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, " pingfang sc cambria cochin georgia times new roman serif left normal auto rgba> <p mp-original-font-size="16" mp-original-line-height="26" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;font-size: 16px;color: black;line-height: 26px;">若从技术的细节来看,shell会依据IFS(Internal Field Seperator) 将command line所输入的文字拆解为"字段"(word/field)。然后再针对特殊字符(meta)先作处理,最后重组整行command line。🎜🎜</p> <h3 data-tool="mdnice编辑器" 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>3. 别人echo, 你也echo, 是问echo知多少?</h3> <blockquote data-tool="mdnice编辑器" mp-original-font-size="14.399999618530273" mp-original-line-height="23" style="margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;outline: 0px;border-left-color: rgba(0, 0, 0, 0.4);color: rgb(106, 115, 125);font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, " pingfang sc cambria cochin georgia times new roman serif left normal auto rgba><p mp-original-font-size="16" mp-original-line-height="26" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;font-size: 16px;color: black;line-height: 26px;"><code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: " operator mono consolas monaco menlo monospace break-all>echo</code>将<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: " operator mono consolas monaco menlo monospace break-all>argument</code>送到<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: " operator mono consolas monaco menlo monospace break-all>标准输出(stdout)</code>,通常显示在屏幕🎜</p></blockquote> <ul class="list-paddingleft-1" data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="25" style='margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 25px;'> <li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">stdin 标准输入</section></li> <li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">stdout 标准输出</section></li> <li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">stderr 标准错误输出</section></li> </ul><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">echo -n # 取消换行符 echo -e # 启用反斜杠转译</pre><div class="contentsignin">登录后复制</div></div><h3 data-tool="mdnice编辑器" 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, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 32px;">4. ""(双引号)与(单引号)有什么区别?</h3><ul class="list-paddingleft-1" data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="25" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 25px;"><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">hard quote:<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">''</code>(单引号),关闭所有引用<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">''</code>(单引号),关闭所有引用</section></li><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">soft quote:<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">""</code>(双引号),保留<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">$</code></section></li></ul>soft quote:<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">""</code>(双引号),保留<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">$</code>引用<h3 data-tool="mdnice编辑器" 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, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 32px;">🎜🎜🎜5. var=value? export前后差在哪?</h3><blockquote data-tool="mdnice编辑器" mp-original-font-size="14.399999618530273" mp-original-line-height="23" style="margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;outline: 0px;border-left-color: rgba(0, 0, 0, 0.4);color: rgb(106, 115, 125);font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;overflow: auto;background-color: rgba(0, 0, 0, 0.047);line-height: 23px;"><p mp-original-font-size="16" mp-original-line-height="26" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;font-size: 16px;color: black;line-height: 26px;">变量定义:<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">name=value</code>,等号左右两边不能使用分隔符。<br mp-original-font-size="16" mp-original-line-height="26" style="outline: 0px;line-height: 26px;"/>变量替换:<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">echo ${name}</code><br mp-original-font-size="16" mp-original-line-height="26" style="outline: 0px;line-height: 26px;"/>export变量:<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">export name=value</code>,使变量成为环境变量</p></blockquote><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"># 本地变量 A=B # 取消变量 unset A # 环境变量 export A=B</pre><div class="contentsignin">登录后复制</div></div><h3 data-tool="mdnice编辑器" 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, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 32px;">6. exec跟source差在哪?</h3><blockquote data-tool="mdnice编辑器" mp-original-font-size="14.399999618530273" mp-original-line-height="23" style="margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;outline: 0px;border-left-color: rgba(0, 0, 0, 0.4);color: rgb(106, 115, 125);font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;overflow: auto;background-color: rgba(0, 0, 0, 0.047);line-height: 23px;"><p mp-original-font-size="16" mp-original-line-height="26" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;font-size: 16px;color: black;line-height: 26px;">环境变量只能从父进程到子进程单向传递。换句话说:在子进程中环境如何变更,均不会影响父进程的环境。<br mp-original-font-size="16" mp-original-line-height="26" style="outline: 0px;line-height: 26px;"/>当我们执行一个shell script时,其实是先产生一个sub-shell的子进程, 然后sub-shell再去产生命令行的子进程。另外,搜索公众号Linux就该这样学后台回复“git书籍”,获取一份惊喜礼包。</p></blockquote><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"># 创建子shell执行脚本 ./sh # 当前shell执行 source sh # 当前shell执行后退出 exec sh</pre><div class="contentsignin">登录后复制</div></div><h3 data-tool="mdnice编辑器" 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, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 32px;">7. ( ) 与 { } 差在哪?</h3><p data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="26" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 26px;"><code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">( )</code><span> </span>将<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">command group</code>置于<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">sub-shell</code>执行<br mp-original-font-size="16" mp-original-line-height="26" style="outline: 0px;line-height: 26px;"/><code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">{ }</code><span> </span>则是在同一个<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">shell</code>内完成</p><h3 data-tool="mdnice编辑器" 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, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 32px;">8.<span> </span><span mp-original-font-size="20" mp-original-line-height="32" style="outline: 0px;cursor: pointer;line-height: 32px;"><span data-formula="(()) 与 " data-formula-type="inline-equation" mp-original-font-size="20" mp-original-line-height="32" role="presentation" style="outline: 0px;line-height: 32px;"><svg aria-hidden="true" focusable="false" mp-original-font-size="20" mp-original-line-height="32" role="img" style="vertical-align: -0.566ex;width: 6.259ex;height: 2.262ex;line-height: 32px;" viewbox="0 -750 2766.3 1000" xmlns="http://www.w3.org/2000/svg"><g fill="currentColor" mp-original-font-size="20" mp-original-line-height="32" stroke="currentColor" stroke-width="0" style="line-height: 32px;" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math" mp-original-font-size="20" mp-original-line-height="32" style="line-height: 32px;"><g data-mml-node="mo" mp-original-font-size="20" mp-original-line-height="32" style="line-height: 32px;"><path d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z" data-c="28" mp-original-font-size="20" mp-original-line-height="32" style="line-height: 32px;"></path></g><g data-mml-node="mo" mp-original-font-size="20" mp-original-line-height="32" style="line-height: 32px;" transform="translate(389, 0)"><path d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z" data-c="28" mp-original-font-size="20" mp-original-line-height="32" style="line-height: 32px;"></path></g><g data-mml-node="mo" mp-original-font-size="20" mp-original-line-height="32" style="line-height: 32px;" transform="translate(778, 0)"><path d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z" data-c="29" mp-original-font-size="20" mp-original-line-height="32" style="line-height: 32px;"></path></g><g data-mml-node="mo" mp-original-font-size="20" mp-original-line-height="32" style="line-height: 32px;" transform="translate(1167, 0)"><path d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z" data-c="29" mp-original-font-size="20" mp-original-line-height="32" style="line-height: 32px;"></path></g><g data-mml-node="mo" mp-original-font-size="20" mp-original-line-height="32" style="line-height: 32px;" transform="translate(1833.8, 0)"><text data-variant="normal" font-family="serif" font-size="933.8px" mp-original-font-size="933.7999877929688" mp-original-line-height="1494" style="font-size: 933.7999877929688px;line-height: 1494px;" transform="matrix(1 0 0 -1 0 0)">与</text></g></g></g></svg></span></span>() 还有 ${} 差在哪?</h3><ul class="list-paddingleft-1" data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="25" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 25px;"><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);"><code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">$()</code>与 ``(反引号) 都是用来做命令替换用的。</section></li><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);"><code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">${var}</code>与<span> </span><code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">$var</code><span> </span>都是用来做变量替换用的。</section></li></ul><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"># 假设我们定义了一个变量为: file=/dir1/dir2/dir3/my.file.txt # 我们可以用 ${ } 分别替换获得不同的值: # shell字符串的非贪婪(最小匹配)左删除 ${file#*/} # 拿掉第一条 / 及其左边的字符串:dir1/dir2/dir3/my.file.txt ${file#*.} #拿掉第一个 . 及其左边的字符串:file.txt # shell字符串的贪婪(最大匹配)左删除 ${file##*/} # 拿掉最后一条 / 及其左边的字符串:my.file.txt ${file##*.} # 拿掉最后一个 . 及其左边的字符串:txt # shell字符串的非贪婪(最小匹配)右删除: ${file%/*} # 拿掉最后条 / 及其右边的字符串:/dir1/dir2/dir3 ${file%.*} # 拿掉最后一个 . 及其右边的字符串:/dir1/dir2/dir3/my.file # shell字符串的贪婪(最大匹配)右删除: ${file%%/*} # 拿掉第一条 / 及其右边的字符串:(空值) ${file%%.*} # 拿掉第一个 . 及其右边的字符串:/dir1/dir2/dir3/my 记忆的方法为: # 是去掉左边(在键盘上 # 在 $ 之左边) % 是去掉右边(在键盘上 % 在 $ 之右边) 单一符号是最小匹配﹔两个符号是最大匹配。</pre><div class="contentsignin">登录后复制</div></div><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"># shell字符串取子串: ${file:0:5}:提取最左边的 5 个字节:/dir1 ${file:5:5}:提取第 5 个字节右边的连续 5 个字节:/dir2 # shell字符串变量值的替换: ${file/dir/path}:将第一个 dir 提换为 path:/path1/dir2/dir3/my.file.txt ${file//dir/path}:将全部 dir 提换为 path:/path1/path2/path3/my.file.txt # ${}还可针对变量的不同状态(没设定、空值、非空值)进行赋值: ${file-my.file.txt} :假如 $file 没有设定,则使用 my.file.txt 作传回值。(空值及非空值时不作处理) ${file:-my.file.txt} :假如 $file 没有设定或为空值,则使用 my.file.txt 作传回值。 (非空值时不作处理) ${file+my.file.txt} :假如 $file 设为空值或非空值,均使用 my.file.txt 作传回值。(没设定时不作处理) ${file:+my.file.txt} :若 $file 为非空值,则使用 my.file.txt 作传回值。 (没设定及空值时不作处理) ${file=my.file.txt} :若 $file 没设定,则使用 my.file.txt 作传回值,同时将 $file 赋值为 my.file.txt 。 (空值及非空值时不作处理) ${file:=my.file.txt} :若 $file 没设定或为空值,则使用 my.file.txt 作传回值,同时将 $file 赋值为 my.file.txt 。 (非空值时不作处理) ${file?my.file.txt} :若 $file 没设定,则将 my.file.txt 输出至 STDERR。 (空值及非空值时不作处理) ${file:?my.file.txt} :若 $file 没设定或为空值,则将 my.file.txt 输出至 STDERR。 (非空值时不作处理) tips: 以上的理解在于, 你一定要分清楚 unset 与 null 及 non-null 这三种赋值状态. 一般而言, : 与 null 有关, 若不带 : 的话, null 不受影响, 若带 : 则连 null 也受影响.</pre><div class="contentsignin">登录后复制</div></div><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"># 计算shell字符串变量的长度:${#var} ${#var} 可计算出变量值的长度: ${#file} 可得到 27 ,因为 /dir1/dir2/dir3/my.file.txt 刚好是 27 个字节... # bash数组(array)的处理方法 数组: A=(a b c d) 引用数组: ${A[@]} ${A[*]} 访问数组成员 ${A[0]} 计算数组长度 ${#A[@]} ${#A[*]} 数组重新赋值 A[2]=xyz # $(( ))是用来做整数运算的 a=5;b=7;c=2; echo $(( a + b * c))</pre><div class="contentsignin">登录后复制</div></div><h3 data-tool="mdnice编辑器" 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, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 32px;">9.<span> </span><span mp-original-font-size="20" mp-original-line-height="32" style="outline: 0px;cursor: pointer;line-height: 32px;"><svg aria-hidden="true" focusable="false" mp-original-font-size="20" mp-original-line-height="32" role="img" style="vertical-align: -0.452ex;width: 4.499ex;height: 2.149ex;line-height: 32px;" viewbox="0 -750 1988.3 950" xmlns="http://www.w3.org/2000/svg"><g fill="currentColor" mp-original-font-size="20" mp-original-line-height="32" stroke="currentColor" stroke-width="0" style="line-height: 32px;" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math" mp-original-font-size="20" mp-original-line-height="32" style="line-height: 32px;"><g data-mjx-texclass="ORD" data-mml-node="TeXAtom" mp-original-font-size="20" mp-original-line-height="32" style="line-height: 32px;"><g data-mml-node="mo" mp-original-font-size="20" mp-original-line-height="32" style="line-height: 32px;"><path d="M56 347Q56 429 86 498T164 612T270 680T386 705Q522 705 622 603T722 349Q722 126 608 126Q541 126 513 176Q512 177 512 179T510 182L509 183Q508 183 503 177T487 163T464 146T429 132T385 126Q311 126 251 186T190 347Q190 448 251 508T385 568Q426 568 460 548T509 511T531 479H555Q580 479 582 478Q586 477 587 468Q588 454 588 338V260Q588 200 593 182T619 163Q641 163 655 178T674 223T680 273T682 325V330Q682 426 647 500Q611 569 544 618T388 668Q271 668 184 577T96 347Q96 216 180 121T396 26Q421 26 446 28T493 34T535 43T573 52T605 63T629 72T647 80T657 84H716Q722 78 722 74Q722 65 675 45T547 7T392 -11Q255 -11 156 90T56 347ZM274 347Q274 266 308 214T390 162Q420 162 449 182T498 235L504 245V449L498 459Q453 532 387 532Q347 532 311 483T274 347Z" data-c="40" mp-original-font-size="20" mp-original-line-height="32" style="line-height: 32px;"></path></g></g></g></g></svg>与</span>* 区别在哪?</h3><ul class="list-paddingleft-1" data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="25" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 25px;"><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);"><code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">"$@"</code><span> </span>则可得到 "p1" "p2 p3" "p4" 这三个不同的词段</section></li><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);"><code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">"$*"</code><span> </span>则可得到 "p1 p2 p3 p4" 这一整串单一的词段</section></li></ul><h3 data-tool="mdnice编辑器" 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, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 32px;">10. && 与 || 差在哪?</h3><ol class="list-paddingleft-1" data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="25" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 25px;"><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">test命令有两种形式</section></li></ol><ul class="list-paddingleft-1" data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="25" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 25px;"><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">test expression</section></li><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">[ expression ]</section></li></ul><ol class="list-paddingleft-1" data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="25" start="2" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 25px;"><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">bash的test目前支持三种测试对象</section></li></ol><ul class="list-paddingleft-1" data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="25" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 25px;"><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">string:字符串</section></li><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">integer:整数</section></li><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">file:文件</section></li></ul><ol class="list-paddingleft-1" data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="25" start="3" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 25px;"><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">当expression为真是返回 0(true) ,否则返回 非0(false)</section></li></ol><ul class="list-paddingleft-1" data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="25" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 25px;"><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);"><code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;字体大小: 14px;边框半径: 4px;颜色: rgb(30, 107, 184);背景颜色: rgba(27, 31, 35, 0.047);字体系列: "Operator Mono", Consolas , Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">command1 && command2</code><code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">command1 && command2</code><span> </span># command2只有在command1的RV为0(true)的条件下执行。<a data-itemshowtype="0" data-linktype="2" href="//m.sbmmt.com/link/725215ed82ab6306919b485b81ff9615" imgdata="null" imgurl="" linktype="text" style="font-family: -apple-system-font, "system-ui", "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;letter-spacing: 1px;text-align: left;white-space: normal;word-spacing: 1px;caret-color: rgb(62, 62, 62);" tab="innerlink" target="_blank" textvalue="关注Linux中文社区"><span style="outline: 0px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;">关注Linux中文社区</span></a></section></li><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);"><code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">command1 || command2</code> <span># command2 只有在command1的RV为0(true)的条件下执行。< data-itemshowtype="0" data-linktype="2" href="//m.sbmmt.com/link/725215ed82ab6306919b485b81ff9615" imgdata="null" imgurl="" linktype="text" style="font -family: -apple-system-font, "system-ui", "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font -大小:16px;字母间距:1px;文本对齐:左对齐;空白:正常;字间距:1px;插入符号颜色:rgb(62, 62, 62);" tab="innerlink" target="_blank" textvalue="关注Linux中文社区"><span style="outline: 0px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;">关注Linux中文社区</span></a></section></li>🎜🎜<代码 mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size : 14px;边框半径: 4px;颜色: rgb(30, 107, 184);背景颜色: rgba(27, 31, 35, 0.047);字体系列: "Operator Mono", Consolas, Monaco, Menlo,等宽字体;断字:全部断行;行高:26px;">命令1 || command2</code>🎜 🎜#command2只有在command1的RV为非0(false)的条件下执行。</section></li></ul><ol class="list-paddingleft-1" data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="25" start="4" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 25px;"><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">先替换变量再比较</section></li></ol><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">A=123 [ -n "$A" ] && ([ "$A" -lt 100 ] || echo "too big") unset A</pre><div class="contentsignin">登录后复制</div></div><h3 data-tool="mdnice编辑器" 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, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 32px;">11. > 与 < 差在哪?</h3><ul class="list-paddingleft-1" data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="25" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 25px;"><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">0: Standard Input (STDIN)</section></li><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">1: Standard Output (STDOUT)</section></li><li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">2: Standard Error Output (STDERR)</section></li></ul><blockquote data-tool="mdnice编辑器" mp-original-font-size="14.399999618530273" mp-original-line-height="23" style="margin-top: 20px;margin-bottom: 20px;padding: 10px 10px 10px 20px;outline: 0px;border-left-color: rgba(0, 0, 0, 0.4);color: rgb(106, 115, 125);font-size: 14.4px;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;overflow: auto;background-color: rgba(0, 0, 0, 0.047);line-height: 23px;"><p mp-original-font-size="16" mp-original-line-height="26" style="padding-top: 8px;padding-bottom: 8px;outline: 0px;font-size: 16px;color: black;line-height: 26px;">我们可用 < 来改变读进的数据信道(stdin),使之从指定的档案读进。<br mp-original-font-size="16" mp-original-line-height="26" style="outline: 0px;line-height: 26px;"/>我们可用<code mp-original-font-size="14" mp-original-line-height="26" style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;line-height: 26px;">></code><span> </span>来改变送出的数据信道(stdout, stderr),使之输出到指定的档案。</p></blockquote><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">ls my.file no.such.file 1> file.out 2>file.err # 2>&1 就是将stderr并进stdout做输出 ls my.file no.such.file 1> file.out 2>&1 # /dev/null 空 ls my.file no.such.file >/dev/null 2>&1 cat < file > file # 在 IO Redirection 中,stdout 与 stderr 的管道会先准备好,才会从 stdin 读进资料。 # 也就是说,在上例中,> file 会先将 file 清空,然后才读进 < file , # 但这时候档案已经被清空了,因此就变成读不进任何数据了</pre><div class="contentsignin">登录后复制</div></div><h3 data-tool="mdnice编辑器" 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, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 32px;">12. 你要if还是case呢?</h3><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"># if echo -n "Do you want to continue?(Yes/No):" read YN if [ "$YN"=Y -o "$YN"=y -o "$YN"="Yes" -o "$YN"="yes" -o "$YN"="YES"];then echo "continue" else exit 0 fi # case echo -n "Do you want to continue?(Yes/No):" read YN case "$YN" in [Yy]|[Yy][Ee][Ss]) echo "continue" ;; *) exit 0 esac</pre><div class="contentsignin">登录后复制</div></div><h3 data-tool="mdnice编辑器" 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, "Times New Roman", serif;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 32px;">13. for what? while与until差在哪?</h3><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;"># for for ((i=1;i<=10;i++)) do echo "num is $i" done # while num=1 while [ "$num" -le 10 ]; do echo "num is $num" num=$(($num + 1)) done # until num=1 until [ "$num" -gt 10 ]; do echo "num is $num" num=$(($nu + 1)) done</pre><div class="contentsignin">登录后复制</div></div><ul class="list-paddingleft-1" data-tool="mdnice编辑器" mp-original-font-size="16" mp-original-line-height="25" style='margin-top: 8px;margin-bottom: 8px;padding-left: 25px;outline: 0px;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, "Times New Roman", serif;font-size: 16px;text-align: left;white-space: normal;text-size-adjust: auto;line-height: 25px;'> <li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">break 是结束 loop</section></li> <li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">return 是结束 function</section></li> <li style="outline: 0px;line-height: 25px;"><section mp-original-font-size="16" mp-original-line-height="26" style="margin-top: 5px;margin-bottom: 5px;outline: 0px;line-height: 26px;color: rgb(1, 1, 1);">exit 是结束 script/shell</section></li> </ul> </blockquote> </blockquote> </div>