Kad ir labāk izmantot #! / Bin / bash nevis #! / Bin / sh vietā Shell Script?

Satura rādītājs:

Kad ir labāk izmantot #! / Bin / bash nevis #! / Bin / sh vietā Shell Script?
Kad ir labāk izmantot #! / Bin / bash nevis #! / Bin / sh vietā Shell Script?

Video: Kad ir labāk izmantot #! / Bin / bash nevis #! / Bin / sh vietā Shell Script?

Video: Kad ir labāk izmantot #! / Bin / bash nevis #! / Bin / sh vietā Shell Script?
Video: CS50 2015 - Week 7, continued - YouTube 2024, Maijs
Anonim
Kad jūs izveidojat jaunu čaulas skriptu, jūs vēlaties pārliecināties, vai tas ir bez problēmām, bet dažreiz tas var būt nedaudz mulsinošs, lai uzzinātu, kura shebang ir labākā lieta, ko jūs varat izmantot. Šajā piezīmē šodienas SuperUser Q & A ziņai ir atbilde uz sajaukt lasītāja jautājumu.
Kad jūs izveidojat jaunu čaulas skriptu, jūs vēlaties pārliecināties, vai tas ir bez problēmām, bet dažreiz tas var būt nedaudz mulsinošs, lai uzzinātu, kura shebang ir labākā lieta, ko jūs varat izmantot. Šajā piezīmē šodienas SuperUser Q & A ziņai ir atbilde uz sajaukt lasītāja jautājumu.

Šodienas jautājumu un atbilžu sesija mums priecājas par SuperUser - Stack Exchange dalību, kas ir kopienas vadīta Q & A tīmekļa vietņu grupa.

Jautājums

SuperUser lasītājs Hendre vēlas uzzināt, kad tas ir labāk izmantot #! / bin / bash tā vietā #! / bin / sh čaulas skriptos:

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

Kad tas ir labāk izmantot #! / bin / bash tā vietā #! / bin / sh čaulas skriptā?

Atbilde

SuperUser ieguldītājs gravitāte ir atbilde mums:

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.

Vai kaut ko pievienot paskaidrojumam? Skatieties komentāros. Vēlaties lasīt citas atbildes no citiem tehnoloģiju savvy Stack Exchange lietotājiem? Šeit skatiet pilnu diskusiju pavedienu.

Attēlu kredīts: Wikipedia

Ieteicams: