Where is sh?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Where is sh?
# 1  
Old 06-09-2014
Where is sh?

Hello,

I know is advised to write a shell script in sh so it's portable to all unix/linux systems.

I know that /bin/sh was a symbolic link to /bin/bash (in rhel) and /bin/sh is a symbolic link to /bin/dash (ubuntu and debian).

How can I execute a script with old sh, not bash or dash?

man bash says:

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.

But since there is no binary executable for sh in the system (?) this must be an internal behaviour of bash.

So how can I execute a script using old sh?

K.
# 2  
Old 06-09-2014
Quote:
Originally Posted by Kibou
Hello,

I know is advised to write a shell script in sh so it's portable to all unix/linux systems.

I know that /bin/sh was a symbolic link to /bin/bash (in rhel) and /bin/sh is a symbolic link to /bin/dash (ubuntu and debian).

How can I execute a script with old sh, not bash or dash?
BASH, DASH, or KSH should all be able to run scripts written for plain Bourne just fine.

If it's not kept in /bin/sh, the system in question probably doesn't have an old Bourne.
# 3  
Old 06-09-2014
Quote:
Shell boy: Do not try and bend the shell. That's impossible. Instead only try to realize the truth.
Kibou: What truth?
Shell boy: There is no shell.
Kibou: There is no shell?
Shell boy: Then you'll see that it is not the shell that bends, it is only yourself.
# 4  
Old 06-09-2014
Quote:
Originally Posted by Kibou
I know is advised to write a shell script in sh so it's portable to all unix/linux systems.
If you need to write a portable script, you have to stick with a POSIX compliant shell and also only use POSIX compliant utilities. You should also restrict your syntax and options to the one decribed in the POSIX documentation. Despite a common confusion due to the fact they both share the same name (sh), the POSIX shell is not the traditional legacy Bourne shell but was created from the Korn shell.
Quote:
I know that /bin/sh was a symbolic link to /bin/bash (in rhel) and /bin/sh is a symbolic link to /bin/dash (ubuntu and debian).

How can I execute a script with old sh, not bash or dash?
You probably don't need to. Just use dash or bash in POSIX mode.

Should you really want for some reason to run the legacy bourne shell, you can get it here The Heirloom Bourne Shell
This User Gave Thanks to jlliagre For This Post:
# 5  
Old 06-10-2014
Quote:
Originally Posted by jlliagre
If you need to write a portable script, you have to stick with a POSIX compliant shell and also only use POSIX compliant utilities. You should also restrict your syntax and options to the one decribed in the POSIX documentation. Despite a common confusion due to the fact they both share the same name (sh), the POSIX shell is not the traditional legacy Bourne shell but was created from the Korn shell.
You probably don't need to. Just use dash or bash in POSIX mode.

Should you really want for some reason to run the legacy bourne shell, you can get it here The Heirloom Bourne Shell
So if my shebang is #!/bin/sh but in both systems /bin/sh -> /bin/somewherelese (bash or dash), how do I run it in POSIX mode?
# 6  
Old 06-10-2014
Code:
#!/bin/bash --posix

Code:
#!/bin/sh --posix

Code:
bash --posix shell_program.sh

Code:
sh --posix shell_program.sh

# 7  
Old 06-10-2014
Quote:
Originally Posted by Aia
Code:
#!/bin/bash --posix

mmm.. according to man bash, if I call a script with:

Code:
sh myscript.sh

it will behave the same way as well.

So this means that even there is no /bin/sh executable, the way to execute the bourne shell is through an internal behaviour of bash (--posix suffix) or calling the script like (sh myscript.sh).
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question