How doesbinbash execute a shell scriptSolutionThe shebang is

How does/bin/bash execute a shell script?

Solution

The shebang is a directive to the loader to use the program which is specified after the #! as the interpreter for the file in question when you try to execute it.

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.

If you try to run a file called foo.sh which has #!/bin/bash at the top, the actual command that runs is /bin/bash foo.sh.

This is a flexible way of using different interpreters for different programs. This is something implemented at the system level and the user level API is the shebang convention.

It\'s also worth knowing that the shebang is a magic number - a human readable one that identifies the file as a script for the given interpreter.

Your point about it \"working\" even without the shebang is only because the program in question is a shell script written for the same shell as the one you are using. For example, you could very well write a javascript file and then put a #! /usr/bin/js (or something similar) to have a javascript \"Shell script\".

Executing a shell script:-

Once a shell script is created, there are serveral way to execute it. Howerever, before executing a shell script you must assign proper permissions using chmod command you can change file permissions.

For example:-

For example giving execute permission to the file temp

Hope it will help to solve your qurey.

 How does/bin/bash execute a shell script?SolutionThe shebang is a directive to the loader to use the program which is specified after the #! as the interpreter

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site