Unix Shell Scripts   «Prev 

Self-commenting Code

As you learn about different shell scripting elements, you can help make your code easy to understand by using variable names and function names that are descriptive.
Do not hesitate to use names longer than just three to five characters. For example, the variable name count may do the job, but using a variable name of flight_number is much more helpful. Functions (which you will learn about later in the course) enable you to name an entire block of statements and refer to them by one name. Make that name as descriptive as possible.
As you write more complex scripts, establish a pattern of naming variables, functions, and even shell script files. Because UNIX is case sensitive, names such as flightnumber, FlightNumber, and FLIGHTNUMBER are all different. Choose one method of naming items using embedded capital letters, all lowercase, with an underscore between words, or something similar. This will help you keep track of the words you have used in your shell scripts and avoid errors as you create larger projects.

Environment variables

There are many environment variables that change the way the system works. You can set these interactively, or more usefully in your ~/.bashrc file.

PS1 Prompt

PS1 is the basic shell prompt; you can customize this. The default for bash is \s-\v\$, or 'shellversion- dollar' for example, bash-4.1$.
Numerous settings are available, see the Prompting section of the bash man page for the full list. A common value for PS1 is

this displays the login name, the server name, and the current working directory. The following example:
shows that you are logged in to the server 'goldie' as the user 'steve,' and are currently in the
/var/log directory.
In Debian, the default ~/.bashrc allows for color in the PS1 prompt, but it also comments that the focus in a terminal window should be on the output of commands, not on the prompt. You can uncomment the force_color_prompt=yes line in that file if you really do want a color prompt.


You can set your PATH environment variable to tell the shell where to search for programs (and scripts) to be run. The main system commands are in /bin, /usr/bin, /sbin, and /usr/sbin, but you may have your own scripts in $HOME/bin, $HOME/scripts, /usr/local/bin, or elsewhere. Append these to the PATH so that they will be found by the shell even when you are not in that directory:

Without the PATH, you will need to provide an explicit path (either absolute or relative) to the command.
For example:
$ myscript.sh
bash: myscript.sh: command not found
$ /home/steve/bin/myscript.sh
... or:
$ cd /home/steve/bin
$ ./myscript.sh