为什么需要将#!/bin/bash 放在脚本文件的开头? [英] Why do you need to put #!/bin/bash at the beginning of a script file?

查看:32
本文介绍了为什么需要将#!/bin/bash 放在脚本文件的开头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前制作过 Bash 脚本,它们在没有 #!/bin/bash 开头.

I have made Bash scripts before and they all ran fine without #!/bin/bash at the beginning.

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

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

另外,你如何发音#?我知道 ! 的发音是bang".

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

#! 如何发音?

推荐答案

这是一个约定,所以 *nix shell 知道要运行什么样的解释器.

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

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

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

即使在今天(大多数系统都运行 bash,Bourne Again Shell"),脚本可以是 bash、python、perl、ruby、PHP 等.例如,您可能参见 #!/bin/perl#!/bin/perl5.

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.

附注:感叹号 (!) 被亲切地称为 bang".shell 注释符号 (#) 有时称为 hash".

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

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

PPS: Remember - under *nix, associating a suffix with a file type is merely a convention, not a "rule". 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 放在脚本文件的开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆