<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;"></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;">經典的Shell十三問<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;"></p></li> <li style="outline: 0px;visibility: visible;line-height: 25px;">#1. 為何叫做shell?<p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;"></p> </li> <li style="outline: 0px;visibility: visible;line-height: 25px;">2 . Shell prompt(PS1)與Carriage Return(CR)的關係?<p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;"></p> </li> <li style="outline: 0px;visibility: visible;line-height: 25px;">#3. 別人echo, 你也echo, 是問echo知多少?<p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;"></p> </li> <li style="outline: 0px;visibility: visible;line-height: 25px;">#4. ""(雙引號)與(單引號)有什麼差別?<p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;"></p> </li> <li style="outline: 0px;visibility: visible;line-height: 25px;">#5. var=value? export前後差在哪? <p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;"></p> </li> <li style="outline: 0px;visibility: visible;line-height: 25px;">6. exec跟source差在哪? <p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;"></p>######7. ( ) 與 { } 差在哪? ###</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;"></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;"> </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;"></p> </li> <li style="outline: 0px;visibility: visible;line-height: 25px;">10. && 與 || 差在哪裡? <p mp-original-font-size="16" mp-original-line-height="25" style="outline: 0px;visibility: visible;line-height: 25px;"></p>######11. > 與 < 差在哪裡? ############12. 你要if還是case呢? ############13. for what? while與until差在哪? ###</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;word-break: break-all;line-height: 26px;'>CR(Carriage Return)</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);">#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></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>(單引號),關閉所有引用</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><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;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> </span># 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> </div>