Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-15-2008
Registered User
 

Join Date: Aug 2008
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 09-16-2008
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Thanks: 0
Thanked 2 Times in 2 Posts
/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.
Sponsored Links
    #3  
Old 09-16-2008
Registered User
 

Join Date: Aug 2008
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
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
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 7 Times in 7 Posts
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.
Sponsored Links
    #5  
Old 09-16-2008
radoulov's Avatar
--
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 5,123
Thanks: 92
Thanked 426 Times in 401 Posts
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 01:30 PM..
Sponsored Links
    #6  
Old 09-16-2008
Registered User
 

Join Date: Aug 2008
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
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?
Sponsored Links
    #7  
Old 09-16-2008
radoulov's Avatar
--
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 5,123
Thanks: 92
Thanked 426 Times in 401 Posts
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.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -4. The time now is 05:14 AM.