LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   What are the meaning and the relevance of #!/bin/bash? (https://www.linuxquestions.org/questions/linux-newbie-8/what-are-the-meaning-and-the-relevance-of-bin-bash-936856/)

ravi_nandula 03-28-2012 06:17 AM

What are the meaning and the relevance of #!/bin/bash?
 
Hi evryone,

Just now I got a doubt that .......in the starting for the script we write....
#!/bin/bash

what is the use of it...........only to mention that it is bash script ?????????

catkin 03-28-2012 06:20 AM

It's known as a shebang and is very useful. More info on Wikipedia.

ravi_nandula 03-28-2012 06:29 AM

Quote:

Originally Posted by catkin (Post 4638605)
It's known as a shebang and is very useful. More info on Wikipedia.

So from the wikipedia link .....I came to understand is .....No use of writing the #!/bin/bash....
since it takes it as a comment......am I rite???????????

linuxlover.chaitanya 03-28-2012 06:34 AM

Why dont you try and omit the line from some script yourself and try?

ravi_nandula 03-28-2012 06:48 AM

Quote:

Originally Posted by linuxlover.chaitanya (Post 4638616)
Why dont you try and omit the line from some script yourself and try?

hi chaitanya,

I have tryed it.....no changes.so finally there is no use of writing it.
We can proceed without using that..............

colucix 03-28-2012 07:08 AM

Try this:
Code:

#!/bin/bash
BEGIN {

  print "Hello world!"

}

and this
Code:

#!/bin/awk -f
BEGIN {

  print "Hello world!"

}

and this
Code:

#!/bin/tcsh
BEGIN {

  print "Hello world!"

}

Do you see any difference? You've already been given advices on reading some good reference about shell scripting in another thread. Did you care to examine it?

catkin 03-28-2012 11:42 AM

Quote:

Originally Posted by ravi_nandula (Post 4638611)
So from the wikipedia link .....I came to understand is .....No use of writing the #!/bin/bash....
since it takes it as a comment......am I rite???????????

No, you are not

suicidaleggroll 03-28-2012 11:52 AM

The reason removing it doesn't change anything is because you're already running a bash shell. If you ran a tcsh, sh, csh, etc. shell then WITHOUT that line at the top, it would try to execute your script in whatever shell you were using. By putting that line at the top, you're forcing it to use bash. This isn't an issue if you use bash, and everybody who's going to use the script uses bash, but if you sent the script to somebody who used tcsh all of a sudden the script would break, because the commands you use aren't valid tcsh commands.

pan64 03-28-2012 12:06 PM

just a comment to it: there is a trick in unix called magic. It means that the first two characters/bytes have a special meaning, those are the magic bytes. They can describe the type of the file. In the case the magic chars are #! it means the following string is an executable to be used to interpret the current file as script. So #!/bin/bash means that OS will invoke bash and feed him with the current file.
Similar: #!/usr/bin/local/perl [flags] will invoke perl and execute the script by perl.
If the first two bytes cannot be identified the script will be processed by the actual shell - if possible. So entering anything before #! (therefore they are not the very first two bytes - for example there is a newline before them) will kill this feature.

The usual practice when you write a script is you will start it with #!<path to the interpreter> <flags>

just a comment to it, windows have this feature also, the first two bytes identify the executables (MZ) and others also...

David the H. 03-29-2012 07:31 AM

As explained, when you execute a script, the shebang tells the system which program to interpret it with. If the line is /bin/bash, then it will be processed by bash, and if it has /bin/awk, or /bin/perl, or /bin/python, it will be processed by those programs.

Note that this only applies when you execute the script directly. If you instead manually feed the script into an interpreter, the leading # generally means it will be ignored as a comment (depending on how the interpreter is designed).

Code:


$ ./myscript                #executes the interpreter in the shebang to process the script.

$ bash <./myscript        #executes bash first, and feeds it the script to interpret.
                        #the shebang is ignored.


In addition, note that #!/bin/sh is a bit special. When it's used as the shebang, the script is treated as a posix-compliant script, regardless of the actual interpreter used. That is, your system could be set up to use bash as the default interpreter, or dash, or ksh, or one of several other shells that offer posix support, but if your script starts with /bin/sh, they will all treat it identically, as long as it contains only syntax defined by the posix standard.

This means that if you want to use all of the advanced features offered specifically by bash (e.g. arrays or extended globbing), be sure to use #!/bin/bash, and NOT #!/bin/sh as your first line.


All times are GMT -5. The time now is 02:49 PM.