Kapan Lebih Baik Menggunakan #! / Bin / bash Daripada #! / Bin / sh dalam Script Shell?

Daftar Isi:

Kapan Lebih Baik Menggunakan #! / Bin / bash Daripada #! / Bin / sh dalam Script Shell?
Kapan Lebih Baik Menggunakan #! / Bin / bash Daripada #! / Bin / sh dalam Script Shell?

Video: Kapan Lebih Baik Menggunakan #! / Bin / bash Daripada #! / Bin / sh dalam Script Shell?

Video: Kapan Lebih Baik Menggunakan #! / Bin / bash Daripada #! / Bin / sh dalam Script Shell?
Video: Command Prompt shortcuts for Windows 7 - YouTube 2024, April
Anonim
Ketika Anda membuat skrip shell baru, Anda ingin memastikannya sebagai masalah bebas mungkin, tetapi kadang-kadang bisa sedikit membingungkan untuk mengetahui shebang mana yang terbaik untuk Anda gunakan. Pada catatan itu, pos Tanya Jawab SuperUser saat ini memiliki jawaban untuk pertanyaan pembaca yang bingung.
Ketika Anda membuat skrip shell baru, Anda ingin memastikannya sebagai masalah bebas mungkin, tetapi kadang-kadang bisa sedikit membingungkan untuk mengetahui shebang mana yang terbaik untuk Anda gunakan. Pada catatan itu, pos Tanya Jawab SuperUser saat ini memiliki jawaban untuk pertanyaan pembaca yang bingung.

Sesi Tanya & Jawab Hari ini hadir untuk memberi kami SuperUser - subdivisi Stack Exchange, pengelompokan situs web Q & A berbasis komunitas.

Pertanyaan

Pembaca SuperUser Hendre ingin tahu kapan lebih baik digunakan #! / bin / bash dari pada #! / bin / sh dalam skrip shell:

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

Kapan lebih baik digunakan #! / bin / bash dari pada #! / bin / sh dalam skrip shell?

Jawabannya

Penggemar kontributor SuperUser memiliki jawaban untuk kami:

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.

Memiliki sesuatu untuk ditambahkan ke penjelasan? Bicaralah di komentar. Ingin membaca lebih banyak jawaban dari pengguna Stack Exchange yang paham teknologi lainnya? Lihat diskusi lengkap di sini.

Kredit Gambar: Wikipedia

Direkomendasikan: