1. 程式人生 > 實用技巧 >什麼時候在Shell指令碼中使用#!/ bin / bash代替#!/ bin / sh更好?

什麼時候在Shell指令碼中使用#!/ bin / bash代替#!/ bin / sh更好?

when-is-it-better-to-use-bin-bash-instead-of-bin-sh-in-a-shell-script-00

When you are creating a new shell script, you want to make sure it is as problem free as possible, but sometimes it can be a bit confusing to know which shebang is the best one for you to use. On that note, today’s SuperUser Q&A post has the answer to a confused reader’s question.

在建立新的shell指令碼時,您要確保它儘可能沒有問題,但是有時知道哪個shebang是最適合您使用的指令碼可能會有些混亂。 關於這一點,今天的“超級使用者問答”帖子解答了一個困惑的讀者的問題。

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

今天的“問答”環節由SuperUser提供,它是Stack Exchange的一個分支,該社群是由社群驅動的Q&A網站分組。

問題 (The Question)

SuperUser reader Hendre wants to know when it is better to use #!/bin/bash

instead of #!/bin/sh in shell scripts:

超級使用者閱讀器Hendre想知道何時在shell指令碼中使用#!/ bin / bash而不是#!/ bin / sh更好:

When is it more appropriate to use #!/bin/bash rather than #!/bin/sh in a shell script?

什麼時候在shell指令碼中使用#!/ bin / bash而不是#!/ bin / sh更合適?

When is it better to use #!/bin/bash instead of #!/bin/sh in a shell script?

什麼時候在Shell指令碼中使用#!/ bin / bash代替#!/ bin / sh更好?

答案 (The Answer)

SuperUser contributor grawity has the answer for us:

超級使用者貢獻者的感謝為我們提供了答案:

In short:

簡而言之:

  • There are several shells which implement a superset of the POSIX sh specification. On different systems, /bin/sh might be a link to ash, bash, dash, ksh, zsh, etc. It will always be sh-compatible though, never csh or fish.

    有幾個Shell實現POSIX sh規範的超集。 在不同的系統上, / bin / sh可能是ash,bash,dash,dash,ksh,zsh等的連結。儘管如此,它將始終與sh相容,而不是csh或fish。

  • As long as you stick to sh features only, you can (and probably even should) use #!/bin/sh and the script should work fine, no matter which shell it is.

    只要僅堅持使用sh功能,就可以(甚至可能應該)使用#!/ bin / sh ,無論指令碼位於哪個shell中,指令碼都可以正常工作。

  • If you start using bash-specific features (i.e. arrays), you should specifically request bash because, even if /bin/sh already invokes bash on your system, it might not on everyone else’s system, and your script will not run there. The same applies to zsh and ksh, of course.

    如果您開始使用bash特有的功能(即陣列),則應該特別請求bash,因為即使/ bin / sh已經在系統上呼叫bash,它也可能不在其他人的系統上執行,並且您的指令碼將無法在其中執行。 當然,zsh和ksh也是如此。

  • Even if the script is for personal use only, you might notice that some operating systems change /bin/sh during upgrades. For example, on Debian it used to be bash, but was later replaced with the very minimal dash. Scripts which used bashisms but had #!/bin/sh suddenly broke.

    即使該指令碼僅供個人使用,您也可能會注意到某些作業系統在升級過程中會更改/ bin / sh 。 例如,在Debian上它曾經是bash,但後來被極小的破折號所取代。 使用bashisms但具有#!/ bin / sh的指令碼突然中斷。

However:

然而:

  • Even #!/bin/bash is not that correct. On different systems, bash might live in /usr/bin, /usr/pkg/bin, or /usr/local/bin.

    甚至#!/ bin / bash也不是正確的。 在不同的系統上,bash可能位於/ usr / bin/ usr / pkg / bin/ usr / local / bin中

  • A more reliable option is #!/usr/bin/env bash, which uses $PATH. Although the env tool itself is not strictly guaranteed either, /usr/bin/env still works on more systems than /bin/bash does.

    一個更可靠的選項是#!/ usr / bin / env bash ,它使用$ PATH 。 儘管也沒有嚴格保證env工具本身,但是/ usr / bin / env仍比/ bin / bash在更多的系統上工作。



Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.

有什麼補充說明嗎? 在評論中聽起來不錯。 是否想從其他精通Stack Exchange的使用者那裡獲得更多答案? 在此處檢視完整的討論執行緒

Image Credit: Wikipedia

圖片來源:維基百科

翻譯自: https://www.howtogeek.com/276607/when-is-it-better-to-use-bin-bash-instead-of-bin-sh-in-a-shell-script/