# Sh, Bash, Perl, . . .



## sniper007 (Mar 1, 2010)

Hi!

I'm just curious why for most scripting sh language and not bash ? So when use bash and when sh for write some script ?


----------



## graudeejs (Mar 1, 2010)

well It's MUCH faster than BASH
It's de facto standard in Unix


----------



## vermaden (Mar 1, 2010)

@sniper007  

POSIX sh(1) is portable, you will be able to run these scripts on FreeBSD, AIX, Solaris, HP-UX, Linux and even CYGWIN under Windows, as killasmurf86 already mentioned, many times faster then bash(1), also, you do not gain anything by writing scripts in bash(1), you only lose, if you need to create more advanced programming structures, then use python(1).


----------



## graudeejs (Mar 1, 2010)

I forgot to say that sh is also de jure standard in Unix


----------



## Oko (Mar 1, 2010)

sniper007 said:
			
		

> Hi!
> 
> I'm just curious why for most scripting sh language and not bash ? So when use bash and when sh for write some script ?



Because sh is POSIX standard shell. You want your scripts to run on any POSIX system don't you? Use Bash interactively if at all. If I was using FreeBSD I would use mksh (Mir Korn Shell) which is more or less OpenBSD version of pksh (Public Korn Shell). Lot of people like also zsh.  

Korn Shell is excellent for scripting as well but there is the same issue of portability like with Bash.


----------



## fronclynne (Mar 1, 2010)

Just to highlight:


			
				vermaden said:
			
		

> *POSIX sh(1) is portable*



If your script is only ever going to be run on your machine(s), and you know exactly what is (always) available, write it in anything you want.

If you want to be a good egg and contribute to the community, write it in sh(1) so your friends can use (and debug) it.


----------



## graudeejs (Mar 1, 2010)

Oko said:
			
		

> Korn Shell is excellent for scripting as well but there is the same issue of portability like with Bash.



AFAIK any script written in sh will work with mksh
However not all scripts written in mksh will work on sh, that because mksh has some extra features, that sh doesn't have..... However as I said before it's 100% sh backwards compatible.


Btw, isn't sh replaced with ksh (or was it mksh) in OpenBSD?


----------



## Oko (Mar 2, 2010)

killasmurf86 said:
			
		

> AFAIK any script written in sh will work with mksh
> However not all scripts written in mksh will work on sh, that because mksh has some extra features, that sh doesn't have..... However as I said before it's 100% sh backwards compatible.
> 
> 
> Btw, isn't sh replaced with ksh (or was it mksh) in OpenBSD?


Default shell on OpenBSD is OpenBSD version of public Korn Shell with custom OpenBSD patches.

```
# echo $SHELL                                                           
/bin/ksh
$ echo $SHELL
/bin/ksh
```

MirOS Korn Shell is Thorsten Glaser's version of OpenBSD version of public Korn Shell.
For scripting you of course will use sh as on any other system.
These are the shells installed on my OpenBSD machine.

```
$ more /etc/shells
#       $OpenBSD: shells,v 1.8 2009/02/14 17:06:40 sobrado Exp $
#
# list of acceptable shells for chpass(1).
# ftpd(8) will not allow users to connect who are not using
# one of these shells, unless the user is listed in /etc/ftpchroot.
/bin/sh
/bin/csh
/bin/ksh
/usr/local/bin/bash
```
BASH is installed as a dependency when I was building software using ports. As you know
some ports use BASH scripts for its configuration. Nobody in right mind would use BASH on 
OpenBSD.

As of backward compatibility of pksh with sh, sure it exists but the whole point of using 
Korn shell is to be able to use those extra features not present in sh. 
Actually using Korn for shell scripting is not such a strange thing on proprietary Unixes.


----------



## lme@ (Mar 2, 2010)

vermaden said:
			
		

> @sniper007
> 
> POSIX sh(1) is portable, you will be able to run these scripts on FreeBSD, AIX, Solaris, HP-UX, Linux and even CYGWIN under Windows [...]



Unfortunately Solaris' sh(1M) is not POSIX compatible.


----------



## vermaden (Mar 2, 2010)

@lme

Thanks, good to know, but I have never had any problems with scripts on Solaris, propably I did not used functionality that is out POSIX on Solaris.


----------



## Graaf_van_Vlaanderen (Mar 8, 2010)

How about csh and Tenex C-shell?

When I was designing IC's I remember it was often (together with perl) used to do all sort of configurations of the design tools of Cadence, Mentor Graphics, Synopsis, etc...


----------



## vermaden (Mar 8, 2010)

@Graaf_van_Vlaanderen

Do not use (t)csh for scripting, have you ever tried [CMD=""]1> 2> 2>&1 1>&2[/CMD] redirections under (t)csh?


----------



## Graaf_van_Vlaanderen (Mar 8, 2010)

vermaden said:
			
		

> @Graaf_van_Vlaanderen
> 
> Do not use (t)csh for scripting, have you ever tried [CMD=""]1> 2> 2>&1 1>&2[/CMD] redirections under (t)csh?



Mostly I use only small scripts.
I remember it took me a while to find an equivalent for:


```
$ application 2> /dev/null &
```

in csh


```
% applicaction >& /dev/null &
```

But what about performance? How does csh differs from sh?


----------



## graudeejs (Mar 8, 2010)

sh is the fastest shell,
I remember I was reading some simple benchmark (sorry, don't have link)

results were like this:
1) sh (fastest)
2) mksh
3) csh
4) bash

If my memory ain't failing bash was the slowest (if my memory is failing, that slowest was csh)


----------



## ProFTP (Mar 11, 2010)

perl


----------



## fronclynne (Mar 12, 2010)

> Perl isn't really a swiss army knife. That's more like C. Perl is a large, metallic toolbox containing:
> 
> -a complete set of box-end wrenches in metric and imperial, except 10mm and 3/8"
> -a selection of five machinists' hammers
> ...


_Charles Cazabon_, via http://home.xnet.com/~raven/Sysadmin/ASR.Quotes.html


----------



## lme@ (Mar 16, 2010)

If you like to script in (t)csh read this first:
http://www.grymoire.com/Unix/CshTop10.txt


----------

