Using Shell Scripts
Bash scripts are files containing a series of commands, such as those you have seen so far. They can take arguments, like most of the programs we have seen, and can return output which can be piped into other commands. Their names usually end with the file extension .sh although this is not essential.
Comments
In bash scripts, the hash symbol denotes a comment, a line which the interpreter will not exectute. Comments can be on a line alone, or within a line, in which case everything to the right of the '#' is the comment. For example, try this at the command line
echo First #Second
Shebangs
In some cases, comment lines can be used to hide instructions for some other program. In particular, the first 2 characters of a file are read by the operating system (not the shell). If these match the special pair '#!', called the shebang, derived by shortening hash-bang, the line is used to identify the program to run the script with. This means you can invoke the script just by typing its name.
In bash scripts, this is usually
#!/bin/bash
You may also see Python programs using the same construct.
You may also find scripts starting with
#!/usr/bin/env bash
This uses the env utility to setup an environment and then invokes bash. You can include commands such as [VARNAME]=[VALUE] or --unset=[VARNAME] (remembering to replace VARNAME with what you want, and omit the brackets) in the call to env to setup the correct environment for the script.
Execute Permissions
To run a script, it must be given execute permission. You can do this using the command
chmod u+x [scriptname]
A First Script
Open a file in your favorite text editor called script.sh. Place the text below into the file, along with any other commands you feel like, then save the file. Use chmod to set execute permission and then run the script using ./script.sh
You should see a directory list printed in your shell, followed by a request for input. Enter some text and it will be echo'd back to you. The results of any commands you added will follow.
#!/bin/bash ls
echo -n "Enter some text: " read var #Read user input echo "You wrote " $var
Command Line Arguments
To make your scripts more general, you can pass arguments to them. Here we consider just the simple case of passing a string which is passed to some other program. Inside the script, any arguments passed in are stored in variables $n for n a number. The total number of arguments is in the variable $#. $0 is the name of the script, $1 the first argument, etc. Arguments are divided by spaces, so you need to use quotes if your string can contain spaces. For example, add ls $1 to your script.sh and call it with a directory name. What happens if no argument is given?
More Bash
More detailed scripts are beyond the scope of this short entry, but many resources exist. Bash scripts can contain conditionals, loops, simple (and more complex) arithmetic, and can invoke all sorts of system programs. You can create scripts to save time for a host of simple tasks. For ease of use, you can file them in a directory and add that directory to your $PATH, so you can call your scripts from any directory. If you do this, take care your names don't conflict with other programs or you will get unexpected behaviour.
If you already know a programming language, this link can help you get started with bash scripting. Otherwise, look for one of the many tutorials such as this one.