Quando é melhor usar #! / Bin / bash Em vez de #! / Bin / sh em um script de shell?

Índice:

Quando é melhor usar #! / Bin / bash Em vez de #! / Bin / sh em um script de shell?
Quando é melhor usar #! / Bin / bash Em vez de #! / Bin / sh em um script de shell?

Vídeo: Quando é melhor usar #! / Bin / bash Em vez de #! / Bin / sh em um script de shell?

Vídeo: Quando é melhor usar #! / Bin / bash Em vez de #! / Bin / sh em um script de shell?
Vídeo: Comandos do Prompt do Windows Que Você Deveria Saber - YouTube 2024, Maio
Anonim
Quando você está criando um novo shell script, você quer ter certeza de que ele é o mais livre de problemas possível, mas às vezes pode ser um pouco confuso saber qual shebang é o melhor para você usar. Na mesma nota, a postagem de perguntas e respostas do SuperUser de hoje tem a resposta para uma pergunta confusa do leitor.
Quando você está criando um novo shell script, você quer ter certeza de que ele é o mais livre de problemas possível, mas às vezes pode ser um pouco confuso saber qual shebang é o melhor para você usar. Na mesma nota, a postagem de perguntas e respostas do SuperUser de hoje tem a resposta para uma pergunta confusa do leitor.

A sessão de perguntas e respostas de hoje nos é oferecida por cortesia do SuperUser, uma subdivisão do Stack Exchange, um agrupamento de sites de perguntas e respostas conduzido pela comunidade.

A questão

Leitor SuperUser Hendre quer saber quando é melhor usar #! / bin / bash ao invés de #! / bin / sh em scripts de shell:

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

Quando é melhor usar #! / bin / bash ao invés de #! / bin / sh em um script de shell?

A resposta

O contribuidor do SuperUser, Grawity, tem a resposta para nós:

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.

Tem algo a acrescentar à explicação? Som desligado nos comentários. Quer ler mais respostas de outros usuários do Stack Exchange com experiência em tecnologia? Confira o tópico de discussão completo aqui.

Crédito de imagem: Wikipedia

Recomendado: