Chinaunix首页 | 论坛 | 博客
  • 博客访问: 84129
  • 博文数量: 19
  • 博客积分: 546
  • 博客等级: 中士
  • 技术积分: 232
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-10 00:07
文章分类

全部博文(19)

文章存档

2014年(1)

2013年(3)

2012年(2)

2011年(4)

2010年(9)

我的朋友

分类: Python/Ruby

2012-07-31 21:33:08

Question: 
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?

*********************************************************************************************************************************
Answer:
1. 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.

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.


2. It's a convention so the *nix shell knows what kind of interpreter to runFor 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".

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

阅读(1255) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~