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