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

本文解释了在Bash脚本文件开头使用/bin/bash的原因,以及它如何帮助*ix shell识别脚本的解释器类型。文章还讨论了不同shell的历史背景,以及现代系统中脚本可以使用的多种解释器。

http://stackoverflow.com/questions/8967902/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. What's the point of putting it in? Would things be any different?

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

For example, older flavors of ATT defaulted to "sh" (the Bourne shell), while older versions of BSD defaulted to "csh" (the 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".

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

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".


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值