|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
/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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Consider also the following: Code:
man bash|less -p'name sh' Quote:
Last edited by radoulov; 09-16-2008 at 01:30 PM.. |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
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
|
||||
|
||||
|
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 | ||
|
|