为什么需要在脚本文件的开头加上#!/ bin / bash?

本文翻译自:Why do you need to put #!/bin/bash at the beginning of a script file?

I have made Bash scripts before and they all ran fine without this at the beginning. 之前我已经制作了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? 发音?


#1楼

参考:https://stackoom.com/question/bcxa/为什么需要在脚本文件的开头加上-bin-bash


#2楼

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. 如果未声明并且您的脚本中有一些该系统不支持的函数,则应声明#/ bin / bash。 I've ran into this problem before at work and now I just include it as a practice. 我在工作之前遇到过这个问题,现在我只是把它作为一种练习。


#3楼

Also you will see some other parameters after #!/bin/bash, for example 例如,您还会在#!/ bin / bash之后看到一些其他参数
#!/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 . https://unix.stackexchange.com/questions/124272/what-do-the-arguments-v-and-x-mean-to-bash


#4楼

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/perl or #!/bin/perl5 . 即使在今天(大多数系统运行bash, “Bourne Again Shell” ),脚本也可以是bash,python,perl,ruby,PHP等等。例如,你可能会看到#!/bin/perl#!/bin/perl5

PS: The exclamation mark ( ! ) is affectionately called "bang" . PS:感叹号( ! )被亲切地称为“爆炸” The shell comment symbol ( # ) is sometimes called "hash" . shell注释符号( # )有时称为“哈希”

PPS: Remember - under *nix, associating a suffix with a file type is merely a convention , not a "rule" . PPS:记住 - 在* nix下,将后缀与文件类型相关联仅仅是一种约定 ,而不是“规则” An executable can be a binary program, any one of a million script types and other things as well. 可执行文件可以是二进制程序,也可以是一百万种脚本类型中的任何一种。 Hence the need for #!/bin/bash . 因此需要#!/bin/bash


#5楼

The operating system takes default shell to run your shell script. 操作系统使用默认shell来运行shell脚本。 so mentioning shell path at the beginning of script, you are asking the OS to use that particular shell. 所以在脚本开头提到shell路径,你要求OS使用那个特定的shell。 It is also useful for portability . 它对于便携性也很有用。


#6楼

Every distribution has a default shell. 每个发行版都有一个默认shell。 Bash is the default on the majority of the systems. Bash是大多数系统的默认设置。 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编写的,则脚本可能无法正常工作。

Bash has evolved over the years taking code from ksh and sh . 多年来,Bash从kshsh获取代码。

Adding #!/bin/bash as the first line of your script, tells the OS to invoke the specified shell to execute the commands that follow in the script. 添加#!/bin/bash作为脚本的第一行,告诉操作系统调用指定的shell来执行脚本中的命令。

#! is often referred to as a "hash-bang", "she-bang" or "sha-bang". 通常被称为“哈希砰”,“嘻嘻”或“沙梆”。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值