Kada geriau naudoti #! / Bin / bash vietoj #! / Bin / sh į Shell scenarijų?

Turinys:

Kada geriau naudoti #! / Bin / bash vietoj #! / Bin / sh į Shell scenarijų?
Kada geriau naudoti #! / Bin / bash vietoj #! / Bin / sh į Shell scenarijų?

Video: Kada geriau naudoti #! / Bin / bash vietoj #! / Bin / sh į Shell scenarijų?

Video: Kada geriau naudoti #! / Bin / bash vietoj #! / Bin / sh į Shell scenarijų?
Video: 40 Windows Commands you NEED to know (in 10 Minutes) - YouTube 2024, Gegužė
Anonim
Kai kuriate naują shell scenarijų, jūs norite įsitikinti, kad tai yra kuo daugiau problemų, bet kartais tai gali būti šiek tiek painu, nes žinoma, kas labiausiai jums gali būti naudojama "shebang". Šiame rašte šiandien "SuperUser Q &A" įrašas turi atsakymą į painiavą skaitytojo klausimą.
Kai kuriate naują shell scenarijų, jūs norite įsitikinti, kad tai yra kuo daugiau problemų, bet kartais tai gali būti šiek tiek painu, nes žinoma, kas labiausiai jums gali būti naudojama "shebang". Šiame rašte šiandien "SuperUser Q &A" įrašas turi atsakymą į painiavą skaitytojo klausimą.

Šiandieninė klausimų ir atsakymų sesija pateikiama su "SuperUser" - "Stack Exchange", bendruomenės valdoma Q & A grupių asociacija.

Klausimas

"SuperUser" skaitytuvas "Hendre" nori žinoti, kada jį geriau naudoti #! / bin / bash vietoj #! / bin / sh "shell" scenarijose:

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

Kada geriau naudoti #! / bin / bash vietoj #! / bin / sh į shell scenarijų?

Atsakymas

SuperUser autorius gravitacija turi mums atsakymą:

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.
  • 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.
  • 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.
  • 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.

However:

  • Even #!/bin/bash is not that correct. On different systems, bash might live in /usr/bin, /usr/pkg/bin, or /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.

Ar turite ką nors įtraukti į paaiškinimą? Garsas išjungtas komentaruose. Norite skaityti daugiau atsakymų iš kitų "Tech-savvy Stack Exchange" vartotojų? Patikrinkite visą diskusijų temą čia.

Vaizdo kreditas: Vikipedija

Rekomenduojamas: