| View previous topic :: View next topic |
| Author |
Message |
pc
Joined: Fri Apr 08, 2005 6:12 pm Posts: 24
|
Posted: Sat Oct 14, 2006 4:09 pm Post subject: Write true sh (rather than bash) script on linux |
|
|
Solaris by default has a true "sh" shell.
Linux by default seems to have the sh shell as link to the bash shell.
If I want to write a truely portable script, I would like to ensure that it is "sh" compatible, and does not use any of the extra features from bash.
Can anyone advise of a way of writing a true sh script on linux, that does not seem to come with a true sh shell? |
|
| Back to top |
|
 |
RD LXF regular
Joined: Mon Jul 25, 2005 3:53 am Posts: 272 Location: irc.ixl2.net
|
Posted: Sun Oct 15, 2006 12:46 am Post subject: RE: Write true sh (rather than bash) script on linux |
|
|
google?? _________________ [url=irc://irc.ixl2.org/ixl2]irc.ixl2.org[/url] |
|
| Back to top |
|
 |
Nigel LXF regular

Joined: Fri Apr 08, 2005 9:03 pm Posts: 1141 Location: Gloucestershire, UK
|
Posted: Sun Oct 15, 2006 12:32 pm Post subject: RE: Write true sh (rather than bash) script on linux |
|
|
Actually, Solaris has several shells available, including bash. It's installed by default with Solaris-10, although it may not be available for previous versions.
So if you write a bash script and make sure it has the line
at the top, it should work fine on Linux and Solaris-10 (and OS-X).
Prior to Solaris-10, if I wanted a portable shell script I would use csh - that was the default on some old versions of Unix and appears to be universally available still. _________________ Hope this helps,
Nigel. |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 7994 Location: Warrington, UK
|
Posted: Sun Oct 15, 2006 2:44 pm Post subject: RE: Write true sh (rather than bash) script on linux |
|
|
When bash is executed through the sh link, it behaves as sh. I generally write scripts with a /bin/sh shebang line unless it uses something bash-specific. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
pc
Joined: Fri Apr 08, 2005 6:12 pm Posts: 24
|
Posted: Mon Oct 16, 2006 10:10 pm Post subject: RE: Write true sh (rather than bash) script on linux |
|
|
Thanks for the replies. The solaris is version 9 and bash is not I believe a default.
I didn't realise that bash through an sh link behaves like sh. How does it do that? How does it know it being called through a link with the name "sh" and so behaves differently?
Quick look at man bash gives:
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.
Guess I should have read the help more thoroughly, apologies.
So the simple answer seem to be to start the script with:
#!/bin/sh
and on linux it will interpret it as "true" sh, even though /bin/sh is a link to /bin/bash. |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 7994 Location: Warrington, UK
|
Posted: Mon Oct 16, 2006 10:53 pm Post subject: Re: RE: Write true sh (rather than bash) script on linux |
|
|
| pc wrote: | | I didn't realise that bash through an sh link behaves like sh. How does it do that? How does it know it being called through a link with the name "sh" and so behaves differently? |
Because each program is passed the name it was called with as its first argument. Write a shell script that contains "echo $0", make a couple of symlinks to it and run them to see what happens. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
Steogede LXF regular
Joined: Thu May 04, 2006 6:39 pm Posts: 145
|
Posted: Mon Oct 16, 2006 11:45 pm Post subject: Re: RE: Write true sh (rather than bash) script on linux |
|
|
| pc wrote: | | I didn't realise that bash through an sh link behaves like sh. How does it do that? How does it know it being called through a link with the name "sh" and so behaves differently? |
Did no one ever tell you bash is omniscient? |
|
| Back to top |
|
 |
pc
Joined: Fri Apr 08, 2005 6:12 pm Posts: 24
|
Posted: Wed Oct 18, 2006 10:29 pm Post subject: RE: Re: RE: Write true sh (rather than bash) script on linux |
|
|
nelz,
Thnaks for the explanation. I did indeed create a script and a soft link to that script. $0 was the script name when run directly, and the link name when run via the link. Previously I had taken $0 to always be the script name, which I can see now is not always the case. |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|