Shebang


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shebang
# 1  
Old 09-16-2008
Shebang

Hi,

I am currently writing BASH shell scripts. I am using BASH on a Powerbook G4 running Leopard. Could somebody please explain the difference between
#!/bin/bash and #!/bin/sh?

I have been using the latter (#!/bin/sh), and things have been working fine. But is that the correct one to use for the version of BASH that is on Leopard? Also, I plan on giving the script to somebody running Ubuntu. Which shebang would they want to use?

Mike
# 2  
Old 09-16-2008
/bin/sh is usually just a link to or another copy of bash which is included for legacy Bourne shell scripts. (ETA - I say "usually" when referring to Linux, and probably MacOS, which I haven't used).

If the script you are writing relies on some bash-specific features you should probably refer to /bin/bash instead.
# 3  
Old 09-16-2008
Hi,

Thank you for the reply. So I understand that bash is basically a revamped bourne shell. Does that mean a script written for a bourne shell could be interpreted by a bash shell, but PERHAPS not vice versa? How similar are the two shells?

Also, if /bin/sh is just a link to, or a copy of bash, why would it be necessary to use /bin/bash for scripts that need bash-specific features? It seems like they are the same. Or perhaps is it possible on some machines that /bin/sh is actually the bourne shell?

Mike
# 4  
Old 09-16-2008
Bash is upwards-compatible with "classic" sh, see the manual page for what's changed. It also mentions what's specific to Bash and cannot be expected to work in traditional Bourne shell.

For scripts you only use yourself, it doesn't matter what you put on the shebang line; use whatever works for you. The difference enters when you want to share your scripts with others. It's a useful convention, though, to explicitly use /bin/bash in scripts which you know are only Bash-compatible, and /bin/sh in scripts which you want to be generally Bourne-compatible.

In general, the expectation is that /bin/sh is Bourne-compatible, but on many platforms, it is indeed not Bash.
# 5  
Old 09-16-2008
Consider also the following:

Code:
man bash|less -p'name sh'

Quote:
If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of
sh as closely as possible, while conforming to the POSIX standard as well. When invoked as an inter‐
active login shell, or a non-interactive shell with the --login option, it first attempts to read and
execute commands from /etc/profile and ~/.profile, in that order. The --noprofile option may be used
to inhibit this behavior. When invoked as an interactive shell with the name sh, bash looks for the
variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file
to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from
any other startup files, the --rcfile option has no effect. A non-interactive shell invoked with the
name sh does not attempt to read any other startup files. When invoked as sh, bash enters posix mode
after the startup files are read

Last edited by radoulov; 09-16-2008 at 02:30 PM..
# 6  
Old 09-16-2008
Hi,

Thank you for all the help. I really appreciate it. Just a few last questions:

- Since bash is upward compatible, should I expect any issues with my script if I change the shebang to #!/bin/bash?

- Although I can have my script running when it invokes bash with sh, I wonder if it will work on the system for my client, for whom I am writing the script. That's what makes me think I should just change the shebang to #!/bin/bash because he has indicated he has bash. He is running Ubuntu. I know some systems store bash in different places. Would #!/bin/bash work on his system?
# 7  
Old 09-16-2008
You should consider using the below syntax if you're using bash specific syntax:

Code:
#!/usr/bin/env bash

If your aim is portability you should write a POSIX compliant code.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Scripts without shebang

I see lot of ad-hoc shell scripts in our servers which don't have a shebang at the beginning . Does this mean that it will run on any shell ? Is it a good practice to create scripts (even ad-hoc ones) without shebang ? (16 Replies)
Discussion started by: kraljic
16 Replies

2. Shell Programming and Scripting

Necessity of shebang line

Hi , I know about the shebang line in shell scripting. Just want to know whether is there any difference in execution of the program by keeping and not keeping the shebang line. Because without shebang line also the script is working. correct me if am wrong. Any help on this will be helpful (5 Replies)
Discussion started by: rogerben
5 Replies

3. Shell Programming and Scripting

csh shebang query

What does the "-f" mean in following interpreter code #!/bin/csh -f Thank you (2 Replies)
Discussion started by: animesharma
2 Replies

4. Shell Programming and Scripting

Shebang

If i am not using #! in my script. By default where will be my script running? (6 Replies)
Discussion started by: Kochu77
6 Replies

5. Shell Programming and Scripting

Hyphen char after shebang notation

Hi, I have a trivial question to ask, I am seeing in some shell scripts the '-' (hyphen) character following the first line of shell script (i.e) the shebang notation as follows: #!/bin/sh - #! /bin/bash - what does the hyphen signify? What will happen if it is not given explicitly? (2 Replies)
Discussion started by: royalibrahim
2 Replies

6. Shell Programming and Scripting

The Shebang!

Hi, I always thought that #!/usr/bin/ksh means that the script would be executed in korn shell i.e. when we'll execute the script with this line as the very first line then the shell spawns a korn shell (in this case as we are using #!/usr/bin/ksh ) and the script gets executed. But I am... (7 Replies)
Discussion started by: dips_ag
7 Replies

7. Shell Programming and Scripting

Multiple shebang lines

*** EDIT: I found something close to my solution under an IIS 7 Module Handle.***** (Non-Homework question, simply an ease of use one) Odd question here and maybe its my newness to cgi/Perl, but is it possible to have 2 shebang lines? I write an test a ton of my homework code on my windows... (1 Reply)
Discussion started by: sennex
1 Replies

8. Shell Programming and Scripting

Relacing the shebang line of a file

Can any one tell me how to replace a shebang line of a file using sed? Eg: If a file contains the following shebang line #!C:/InstantRails/ruby/bin/ruby I would like to replace it with #!/usr/local/bin/ruby The shebang line of the file can be obtained from the command cat... (3 Replies)
Discussion started by: linuxnewbe
3 Replies

9. Shell Programming and Scripting

Doubt in shebang line!!

Do we need to include the exclamatory mark in the shebang line??:confused: What if we dont include it??:eek: Actually what shebang line implies when we run a script?? shebang line--> #!/bin/ksh :p (6 Replies)
Discussion started by: nohup
6 Replies
Login or Register to Ask a Question