Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 #!/bin/bash at the beginning.

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

How is #! pronounced?

like image 612
node ninja Avatar asked Jan 23 '12 06:01

node ninja


People also ask

Why did you apply for this position?

'I see the role as a way of developing my career in a forward-thinking/well-established company/industry as…' 'I feel I will succeed in the role because I have experience in/softs skills that demonstrate/ I've taken this course…' 'I believe my skills are well-suited to this job because…”

How do you answer why do you want to work for this company?

“I see this opportunity as a way to contribute to an exciting/forward-thinking/fast-moving company/industry, and I feel I can do so by/with my …” “I feel my skills are particularly well-suited to this position because …” “I believe I have the type of knowledge to succeed in this role and at the company because …”

Why should hire you Example answer?

For starters, I have all the skills and experience listed in the job description, and I'm confident that I can make an immediate impact on your company. It's not just my background in leading successful projects for Fortune 500 companies, but also my passion for the industry that drives me to succeed.


2 Answers

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.

like image 121
paulsm4 Avatar answered Oct 16 '22 10:10

paulsm4


To be more precise the shebang #!, when it is the first two bytes of an executable (x mode) file, is interpreted by the execve(2) system call (which execute programs). But POSIX specification for execve don't mention the shebang.

It must be followed by a file path of an interpreter executable (which BTW could even be relative, but most often is absolute).

A nice trick (or perhaps not so nice one) to find an interpreter (e.g. python) in the user's $PATH is to use the env program (always at /usr/bin/env on all Linux) like e.g.

 #!/usr/bin/env python 

Any ELF executable can be an interpreter. You could even use #!/bin/cat or #!/bin/true if you wanted to! (but that would be often useless)

like image 20
Basile Starynkevitch Avatar answered Oct 16 '22 10:10

Basile Starynkevitch