152

In bash script, what does #!/bin/bash at the 1st line mean ?

UPDATE: Is there a difference between #!/bin/bash and #!/bin/sh ?

Raptor
  • 53,206
  • 45
  • 230
  • 366

3 Answers3

194

That is called a shebang, it tells the shell what program to interpret the script with, when executed.

In your example, the script is to be interpreted and run by the bash shell.

Some other example shebangs are:

(From Wikipedia)

#!/bin/sh — Execute the file using sh, the Bourne shell, or a compatible shell
#!/bin/csh — Execute the file using csh, the C shell, or a compatible shell
#!/usr/bin/perl -T — Execute using Perl with the option for taint checks
#!/usr/bin/php — Execute the file using the PHP command line interpreter
#!/usr/bin/python -O — Execute using Python with optimizations to code
#!/usr/bin/ruby — Execute using Ruby

and a few additional ones I can think off the top of my head, such as:

#!/bin/ksh
#!/bin/awk
#!/bin/expect

In a script with the bash shebang, for example, you would write your code with bash syntax; whereas in a script with expect shebang, you would code it in expect syntax, and so on.

Response to updated portion:

It depends on what /bin/sh actually points to on your system. Often it is just a symlink to /bin/bash. Sometimes portable scripts are written with #!/bin/sh just to signify that it's a shell script, but it uses whichever shell is referred to by /bin/sh on that particular system (maybe it points to /bin/bash, /bin/ksh or /bin/zsh)

sampson-chen
  • 45,805
  • 12
  • 84
  • 81
  • 3
    Thanks! But is there a difference between `#!/bin/bash` and `#!/bin/sh` ? – Raptor Dec 14 '12 at 03:07
  • 7
    @ShivanRaptor `#!/bin/bash` Means run this script in bash. `#!/bin/sh` means run this script in `sh` which is the default unix shell, which might be bash or any other variant like ksh, dash, zsh, etc – Karthik T Dec 14 '12 at 03:10
  • When `bash` is run as `sh`, it behaves differently (more POSIX-like) than when it is run as `bash`. Read the manual; it does cover that detail. – Jonathan Leffler Dec 14 '12 at 03:14
  • @KarthikT it doesn't run the default shell, it runs bash in Bourne Shell mode. (as opposed to Bourne Again Shell, aka. bash) On some older systems it would run Bourne Shell itself. – ocodo Dec 14 '12 at 03:48
  • @Slomojo i see, i assumed it was just a simple symlink.. – Karthik T Dec 14 '12 at 04:48
  • @KarthikT - it does also depend on what you mean by default shell, (although this won't mean the user defaults set in `etc/skel`) - but if we define default as the distro default shell, then usually we'd be correct. `sh` compatibility is there for POSIX compliance, and most people who do advanced shell scripting will aim to stay compliant with `sh` under normal circumstances, while bash4, zsh and others offer pretty advanced additional features, most scripters would resort to Perl, Python, and increasingly Ruby to do more advanced stuff. – ocodo Dec 14 '12 at 06:25
  • @Slomojo I did mean distro default. Thanks for elabourating about the compatibility mode details, thats new to me. – Karthik T Dec 14 '12 at 06:29
  • Your first sentence: _That is called a shebang, it tells the shell what program to interpret the script with, when executed._ is slightly wrong. It doesn't tell the _shell_ what program to execute, but it tells the _kernel_ what program to execute. Moreover, any executable can be in the `#!`. You can try `#!/usr/bin/firefox`, or even if you have a script `myscript` in path `/path/to/script` then `#!/path/to/script/myscript` will work. In this case, `myscript` will be launched with arguments the one given in the shebang line, together with the filename of the file that launched it. – gniourf_gniourf Nov 09 '13 at 11:42
  • What if it isn't included? Is some default picked or will the script error? – runnerpaul Feb 26 '19 at 21:32
20

In bash script, what does #!/bin/bash at the 1st line mean ?

In Linux system, we have shell which interprets our UNIX commands. Now there are a number of shell in Unix system. Among them, there is a shell called bash which is very very common Linux and it has a long history. This is a by default shell in Linux.

When you write a script (collection of unix commands and so on) you have a option to specify which shell it can be used. Generally you can specify which shell it wold be by using Shebang(Yes that's what it's name).

So if you #!/bin/bash in the top of your scripts then you are telling your system to use bash as a default shell.

Now coming to your second question :Is there a difference between #!/bin/bash and #!/bin/sh ?

The answer is Yes. When you tell #!/bin/bash then you are telling your environment/ os to use bash as a command interpreter. This is hard coded thing.

Every system has its own shell which the system will use to execute its own system scripts. This system shell can be vary from OS to OS(most of the time it will be bash. Ubuntu recently using dash as default system shell). When you specify #!/bin/sh then system will use it's internal system shell to interpreting your shell scripts.

Visit this link for further information where I have explained this topic.

Hope this will eliminate your confusions...good luck.

kta
  • 19,412
  • 7
  • 65
  • 47
16

When the first characters in a script are #!, that is called the shebang. If your file starts with #!/path/to/something the standard is to run something and pass the rest of the file to that program as an input.

With that said, the difference between #!/bin/bash, #!/bin/sh, or even #!/bin/zsh is whether the bash, sh, or zsh programs are used to interpret the rest of the file. bash and sh are just different programs, traditionally. On some Linux systems they are two copies of the same program. On other Linux systems, sh is a link to dash, and on traditional Unix systems (Solaris, Irix, etc) bash is usually a completely different program from sh.

Of course, the rest of the line doesn't have to end in sh. It could just as well be #!/usr/bin/python, #!/usr/bin/perl, or even #!/usr/local/bin/my_own_scripting_language.

Joshua D. Boyd
  • 4,808
  • 3
  • 29
  • 44
  • `sh` is usually `bash` running a compatibility mode, this has been true since bash became the default shell for most Linux distro's, and IIRC for a good few years before that, safe to say it's been 15 years, possibly more than 20. – ocodo Dec 14 '12 at 03:51
  • On my Debian and Ubuntu systems, `/bin/sh` is dash. Android says it uses `ash` as `/bin/sh`. – Joshua D. Boyd Dec 14 '12 at 04:39
  • 1
    @JoshuaDBoyd nice catch, many (although probably not all) BSD unixes use Ash, as their default shell, as opposed to Bash, and Debian / Ubuntu use Dash. Likewise with Bash, both Ash & Dash have `sh` compatibility modes. If we delve into Solaris, Aix, HPUX, Ultrix, SCO etc. each will have their own default shell, which will have `sh` emulation (to remain POSIX compliant.) – ocodo Dec 14 '12 at 06:19