Linux 为什么需要将 #!/bin/bash 放在脚本文件的开头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8967902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:10:06  来源:igfitidea点击:

Why do you need to put #!/bin/bash at the beginning of a script file?

linuxbashscriptingshebang

提问by node ninja

I have made Bashscripts before and they all ran fine without #!/bin/bashat the beginning.

我以前制作过Bash脚本,它们一开始都运行良好#!/bin/bash

What's the point of putting it in? Would things be any different?

放进去有什么意义?情况会有所不同吗?

Also, how do you pronounce #? I know that !is pronounced as "bang."

另外,你怎么发音#?我知道这!发音为“bang”。

How is #!pronounced?

怎么#!发音?

采纳答案by paulsm4

It's a convention so the *nix shell knows what kind of interpreter to run.

这是一个约定,因此 *nix shell 知道要运行哪种解释器。

For example, older flavors of ATT defaulted to sh(the Bourne shell), while older versions of BSD defaulted to csh(the C shell).

例如,旧版本的 ATT 默认使用sh(Bourne shell),而旧版本的 BSD 默认使用csh(C shell)。

Even today (where most systems run bash, the "Bourne Again Shell"), scripts can be in bash, python, perl, ruby, PHP, etc, etc. For example, you might see #!/bin/perlor #!/bin/perl5.

即使在今天(大多数系统运行 bash,即“Bourne Again Shell”),脚本也可以是 bash、python、perl、ruby、PHP 等。例如,您可能会看到#!/bin/perl#!/bin/perl5.

PS: The exclamation mark (!) is affectionately called "bang". The shell comment symbol (#) is sometimes called "hash".

PS:感叹号(!)被亲切地称为“bang”。shell 注释符号 ( #) 有时称为“散列”

PPS: Remember - under *nix, associating a suffix with a file type is merely a convention, not a "rule". An executablecan be a binary program, any one of a million script types and other things as well. Hence the need for #!/bin/bash.

PPS:请记住 - 在 *nix 下,将后缀与文件类型相关联只是一种约定,而不是“规则”。一个可执行文件可以是一个二进制程序,一百万脚本类型和其他的东西,以及任何一个。因此需要#!/bin/bash.

回答by Balaswamy Vaddeman

The operating system takes default shell to run your shell script. so mentioning shell path at the beginning of script, you are asking the OS to use that particular shell. It is also useful for portability.

操作系统采用默认 shell 来运行您的 shell 脚本。所以在脚本开头提到 shell 路径,你是在要求操作系统使用那个特定的 shell。它对于便携性也很有用。

回答by jaypal singh

Every distribution has a default shell. Bash is the default on the majority of the systems. If you happen to work on a system that has a different default shell, then the scripts might not work as intended if they are written specific for Bash.

每个发行版都有一个默认的 shell。Bash 是大多数系统的默认设置。如果您碰巧在具有不同默认 shell 的系统上工作,那么如果脚本是专为 Bash 编写的,则它们可能无法按预期工作。

Bash has evolved over the years taking code from kshand sh.

多年来,Bash 从kshsh.

Adding #!/bin/bashas the first line of your script, tells the OS to invoke the specified shellto execute the commands that follow in the script.

添加#!/bin/bash为脚本的第一行,告诉操作系统调用指定的shell来执行脚本中的命令。

#!is often referred to as a "hash-bang", "she-bang" or "sha-bang".

#!通常被称为“hash-bang”、“she-bang”或“sha-bang”。

回答by austin1howard

It's called a shebang. In unix-speak, # is called sharp (like in music) or hash (like hashtags on twitter), and ! is called bang. (You can actually reference your previous shell command with !!, called bang-bang). So when put together, you get haSH-BANG, or shebang.

它被称为shebang。在unix 中,# 被称为sharp(就像在音乐中)或hash(就像twitter 上的hashtags),而! 被称为砰。(您实际上可以使用 !! 引用您之前的 shell 命令,称为 bang-bang)。所以当放在一起时,你会得到 haSH-BANG 或 shebang。

The part after the #! tells Unix what program to use to run it. If it isn't specified, it will try with bash (or sh, or zsh, or whatever your $SHELL variable is) but if it's there it will use that program. Plus, # is a comment in most languages, so the line gets ignored in the subsequent execution.

#! 后面的部分 告诉 Unix 使用什么程序来运行它。如果未指定,它将尝试使用 bash(或 sh、zsh 或任何您的 $SHELL 变量),但如果它在那里,它将使用该程序。另外,# 是大多数语言中的注释,因此该行在后续执行中会被忽略。

回答by Uday Sawant

It is called a shebang. It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash. All scripts under UNIX and Linux execute using the interpreter specified on a first line.

它被称为shebang。它由一个数字符号和一个感叹号字符 (#!) 组成,后跟解释器的完整路径,例如 /bin/bash。UNIX 和 Linux 下的所有脚本都使用第一行指定的解释器执行。

回答by Noufal Ibrahim

The shebangis a directive to the loader to use the program which is specified after the #!as the interpreter for the file in question when you try to execute it. So, if you try to run a file called foo.shwhich has #!/bin/bashat the top, the actual command that runs is /bin/bash foo.sh. This is a flexible way of using different interpreters for different programs. This is something implemented at the system level and the user level API is the shebang convention.

家当是一个指令加载器使用的是后指定的程序#!,当您尝试执行它解释为有问题的文件。因此,如果您尝试运行一个名为foo.shwhich#!/bin/bash位于顶部的文件,则实际运行的命令是/bin/bash foo.sh. 这是对不同程序使用不同解释器的灵活方式。这是在系统级实现的东西,用户级 API 是 shebang 约定。

It's also worth knowing that the shebang is a magic number- a human readable one that identifies the file as a script for the given interpreter.

还值得知道的是,shebang 是一个神奇的数字- 一个人类可读的数字,它将文件标识为给定解释器的脚本。

Your point about it "working" even without the shebang is only because the program in question is a shell script written for the same shell as the one you are using. For example, you could very well write a javascript file and then put a #! /usr/bin/js(or something similar) to have a javascript "Shell script".

即使没有shebang,您关于它“工作”的观点只是因为有问题的程序是为与您正在使用的shell 相同的shell 编写的shell 脚本。例如,您可以很好地编写一个 javascript 文件,然后放置一个#! /usr/bin/js(或类似的东西)来拥有一个 javascript“Shell 脚本”。

回答by Basile Starynkevitch

To be more precise the shebang#!, when it is the first two bytes of an executable(xmode) file, is interpreted by the execve(2)system call (which execute programs). But POSIX specification for execvedon't mention the shebang.

更准确地说,当它是可执行mode)文件的前两个字节时,shebangexecve(2)系统调用(执行程序)解释。但是POSIX 规范没有提到 shebang。#!xexecve

It must be followed by a file path of an interpreter executable (which BTW could even be relative, but most often is absolute).

它后面必须跟一个解释器可执行文件的文件路径(顺便说一句,它甚至可以是相对的,但通常是绝对的)。

A nice trick (or perhaps not so niceone) to find an interpreter (e.g. python) in the user's $PATHis to use the envprogram (always at /usr/bin/envon all Linux) like e.g.

在用户中找到解释器(例如)的一个好技巧(或者可能不是那么好)是使用该程序(在所有 Linux 上始终为python$PATHenv/usr/bin/env

 #!/usr/bin/env python

Any ELF executable can be an interpreter. You could even use #!/bin/cator #!/bin/trueif you wanted to! (but that would be often useless)

任何 ELF 可执行文件都可以是解释器。您甚至可以使用#!/bin/cat#!/bin/true如果您愿意!(但这通常是无用的)

回答by Beardy

It can be useful to someone that uses a different system that does not have that library readily available. If that is not declared and you have some functions in your script that are not supported by that system, you should declare #/bin/bash. I've ran into this problem before at work and now I just include it as a practice.

对于使用没有现成库的不同系统的人来说,它可能很有用。如果没有声明,并且您的脚本中有一些该系统不支持的函数,您应该声明#/bin/bash。我以前在工作中遇到过这个问题,现在我只是把它作为练习。

回答by star

Also you will see some other parameters after #!/bin/bash, for example
#!/bin/bash -v -x
read this to get more idea.
https://unix.stackexchange.com/questions/124272/what-do-the-arguments-v-and-x-mean-to-bash.

您还将在 #!/bin/bash 之后看到一些其他参数,例如
#!/bin/bash -v -x
阅读此内容以获得更多想法。
https://unix.stackexchange.com/questions/124272/what-do-the-arguments-v-and-x-mean-to-bash