# Learning Shell Scripting



## Majorix (Sep 30, 2012)

So I thought it would be great for a CS student like myself to learn Shell Scripting, awk, sed, looping constructs and so on.

However I have no idea where to start.

I have found a tutorial/book called "Linux Shell Scripting Tutorial". Is it relevant to FreeBSD? Which shell does FreeBSD use by default, sh, bash, csh, tcsh, zsh, ..., ?

I am completely puzzled at this point and could use a few pointers.


----------



## wblock@ (Sep 30, 2012)

FreeBSD uses sh(1) (ash) for scripts.  Linux tends to confuse bash with sh.  But bash is a superset, and has many features missing from sh that will bite you if you try to write portable scripts.  So here, we generally advise sticking to sh(1).


----------



## Majorix (Sep 30, 2012)

So I can follow that tutorial? I don't know which shell it describes, but most likely it is bash


----------



## wblock@ (Sep 30, 2012)

Yes, you can follow it.  Just remember that if there are problems, you may need to check whether it is trying to use a bash-specific feature.  Or install shells/bash and use it, but then you won't know when a feature is bash-specific.


----------



## Majorix (Sep 30, 2012)

Thanks, I will read it asap.


----------



## UNIXgod (Sep 30, 2012)

The ash man page is also easy to follow and learn from:


```
%  man bash | wc -l                                 
    5463

%  man sh | wc -l                                    
    1587
```

Classic for study:
http://www.grymoire.com/Unix/


----------



## Majorix (Sep 30, 2012)

Thanks, looks useful.


----------



## Anonymous (Sep 30, 2012)

Majorix said:
			
		

> So I can follow that tutorial? I don't know which shell it describes, but most likely it is bash



The Shell Scripting Primer got a quick but reasonable overview about Shell Script Dialects. You might want to follow the advise in there:



> ... Fortunately, once you get the basics, things generally fall into place as long as you avoid using shell-specific features. Stay on the narrow road and your code will be portable ...


----------



## Majorix (Sep 30, 2012)

I have just done a`$ echo $shell`and it told me I am using csh :S I am afraid root also uses the same. I have the feeling that it would cause problems if I change the shell to sh or bash. What should I do?


----------



## Majorix (Sep 30, 2012)

Sorry for double post.

I am thinking about getting this book:
http://www.amazon.co.uk/Mastering-U...r_1_33?s=books&ie=UTF8&qid=1349035136&sr=1-33
It is for more advanced users, so I won't be "taught" once again what a loop does. And it is not strictly about sh, csh, bash or whatever. Seems like the ultimate book for me


----------



## Anonymous (Sep 30, 2012)

Majorix said:
			
		

> I have just done a`$ echo $shell`and it told me I am using csh :S I am afraid root also uses the same. I have the feeling that it would cause problems if I change the shell to sh or bash...



Changing the shell for a user by itself does not cause problems right away. By default, the root user comes with sh(1)().

The common advise for the root user is, not to change its shell to something outside of the base system AND outside of the boot partition. For example if you set the shell of root to /usr/local/bin/bash, i.e. bash installed from the ports, then it might easily happen that you lock yourself out when /usr would be on a different volume which for any reason cannot be mounted. Also, sometimes it happens that a port update fails, and if a failure would hit bash(1), then the respective users would stay without a working shell.

No risk no fun, so, nonetheless, I set the root's shell to /usr/local/bin/bash. If by any means that shell becomes unusable, I can login as the user toor using sh. In any case, I have ready an usb stick for starting-up an emergency FreeBSD system from which I can mount and repair all affected volumes. 



			
				Majorix said:
			
		

> ... What should I do?



Use sh(1)() for root, and bash(1) for standard users. Change the shell for root to bash only, once you got some *working* emergency measures in place.


----------



## UNIXgod (Sep 30, 2012)

Majorix said:
			
		

> Sorry for double post.
> 
> I am thinking about getting this book:
> http://www.amazon.co.uk/Mastering-U...r_1_33?s=books&ie=UTF8&qid=1349035136&sr=1-33
> It is for more advanced users, so I won't be "taught" once again what a loop does. And it is not strictly about sh, csh, bash or whatever. Seems like the ultimate book for me



I have the first version of that book. In all honesty you could save your money and follow your own path and read what's available online. If you really want to own a book on the subject kernighan/pike "The UNIX Programming Environment" is a well respected read( as well as K&R C Programming Language book ). For interactive usage "From Bash To Z Shell" book is good but may be dated and is zsh heavy( good for practicing history and zsh specific tricks). Though once again online info may be a bit more up to date.

For POSIX aware scripting I have this link which you should bookmark and refer to when you need a "buck stops here" reference for portability without extensions or vender lock-in:

POSIX sh: http://rubyprogrammer.net/~stu/posix/

The above link in combination with the sh(1) man page is enough to get started.

Another website work looking at: http://www.shelldorado.com/


----------



## wblock@ (Oct 1, 2012)

Majorix said:
			
		

> I have just done a`$ echo $shell`and it told me I am using csh :S I am afraid root also uses the same. I have the feeling that it would cause problems if I change the shell to sh or bash. What should I do?



csh(1) is fine for interactive use, some of us actually like it.  But we still write scripts to run in sh(1).  All that it takes is

```
#!/bin/sh
```
at the start of the script.  Then you get the best of both.

PS: don't change root's shell.


----------

