# How to save a shell script



## new_techie (Nov 20, 2007)

Hey,

So I'm new to programming in Linux/Unix. Can anyone tell me how I can write a shell script and then save it? As in write a little programme and then be able to execute it whenever I like? I'm stating the obvious by saying I don't have to type the whole thing each time. Does anyone know a good website for learning how to write scripts?

Thanks all!


----------



## RobLinux (Nov 7, 2007)

new_techie said:


> So I'm new to programming in Linux/Unix. Can anyone tell me how I can write a shell script and then save it? As in write a little programme and then be able to execute it whenever I like? I'm stating the obvious by saying I don't have to type the whole thing each time. Does anyone know a good website for learning how to write scripts?


$ cat > ~/bin/myscript
#!/bin/sh

# My first script
echo Foo bah!
<CNTRL-D>
$ chmod +x !$

To edit in terminal there's usually a simple editor like nano installed, or you can use GUI text editor (avoid word processor as they do dumb things to programs). If you're a touch typist, then vi(1) is the best so long as you don't mind modes and putting in a little effort to learn it, emacs(1) and most other editors like <CNTRL> and <ALT> codes for commands.

Just go to http://tldp.org/ there's Guides, read "Intro to Linux", "Bash Guid for Beginners", "Command Line Tools" and only then "Advanced Bash-Scripting".

Ubuntu's using dash, rather than bash during boot up for greater speed, so actually writing standard Bourne shell and avoiding Bash specific extensions might be an idea. dash is the BSD 4.4 shell, and like much BSD 4.4 stuff had excellent re-implementations of trad UNIX stuff. It's usually not too hard to write stuff portably, or convert from Bash-ism to a standard Bourne script.


----------

