Write true sh (rather than bash) script on linux

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Write true sh (rather than bash) script on linux

Postby pc » Sat Oct 14, 2006 3:09 pm

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?
pc
 
Posts: 24
Joined: Fri Apr 08, 2005 5:12 pm

RE: Write true sh (rather than bash) script on linux

Postby RD » Sat Oct 14, 2006 11:46 pm

google??
RD
LXF regular
 
Posts: 272
Joined: Mon Jul 25, 2005 2:53 am
Location: irc.ixl2.net

RE: Write true sh (rather than bash) script on linux

Postby Nigel » Sun Oct 15, 2006 11:32 am

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
Code: Select all
#!/bin/bash

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.
User avatar
Nigel
LXF regular
 
Posts: 1141
Joined: Fri Apr 08, 2005 8:03 pm
Location: Gloucestershire, UK

RE: Write true sh (rather than bash) script on linux

Postby nelz » Sun Oct 15, 2006 1:44 pm

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.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8493
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

RE: Write true sh (rather than bash) script on linux

Postby pc » Mon Oct 16, 2006 9:10 pm

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.
pc
 
Posts: 24
Joined: Fri Apr 08, 2005 5:12 pm

Re: RE: Write true sh (rather than bash) script on linux

Postby nelz » Mon Oct 16, 2006 9:53 pm

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.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8493
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Re: RE: Write true sh (rather than bash) script on linux

Postby Steogede » Mon Oct 16, 2006 10:45 pm

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?
Steogede
LXF regular
 
Posts: 146
Joined: Thu May 04, 2006 5:39 pm

RE: Re: RE: Write true sh (rather than bash) script on linux

Postby pc » Wed Oct 18, 2006 9:29 pm

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.
pc
 
Posts: 24
Joined: Fri Apr 08, 2005 5:12 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron