LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-28-2012, 06:17 AM   #1
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Rep: Reputation: Disabled
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 ?????????
 
Old 03-28-2012, 06:20 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
It's known as a shebang and is very useful. More info on Wikipedia.
 
Old 03-28-2012, 06:29 AM   #3
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
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???????????
 
Old 03-28-2012, 06:34 AM   #4
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,631

Rep: Reputation: Disabled
Why dont you try and omit the line from some script yourself and try?
 
Old 03-28-2012, 06:48 AM   #5
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by linuxlover.chaitanya View Post
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..............
 
Old 03-28-2012, 07:08 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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?
 
Old 03-28-2012, 11:42 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ravi_nandula View Post
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
 
Old 03-28-2012, 11:52 AM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
 
Old 03-28-2012, 12:06 PM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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...

Last edited by pan64; 03-28-2012 at 12:09 PM. Reason: mistypes
 
Old 03-29-2012, 07:31 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

Last edited by David the H.; 03-29-2012 at 07:35 AM. Reason: minor fixes and additions
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] difference between /bin/bash and /bin/dash and compatibility with POSIX kakk2 Linux - Software 7 11-11-2011 01:17 PM
[SOLVED] /usr/bin/xterm: Could not exec /bin/bash: Permission Denied suheng Linux - Newbie 2 09-24-2010 05:31 PM
meaning of suid on /usr/bin/passwd raj k yadav Linux - Newbie 1 01-08-2009 10:52 AM
? meaning of /bin/ls: .: Input/output error JesseMor Linux - Newbie 11 10-28-2008 08:27 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration