Heiner's SHELLdorado
The Ignorant's Guide to Shell Programming
SHELLdorado - your UNIX shell scripting resource
HomeGood Shell Coding PracticesExample Shell ScriptsShell Scripting LinksShell Scripting Tips+TricksShell Scripting Articles

Articles Home

Do you like writing shell scripts that just work, no matter how? Do you think, that a script that already works cannot be improved? Do you look down on people doing performance tests on their shell scripts? Do you think comments are unnecessary, because shell scripts are more frequently written then read? Then this article is written just for you!

If you answered "no" to the questions above, the Good shell coding practices section of the SHELLdorado may be a better place for you.

Contents:


Use $* instead of "$@"

You probably know the difference between "$@" and $*. Both stand for "all arguments specified on the command line". "$@" does preserve whitespace, while $* does not.

Just ignore this certainly neglectable difference. If users use file names containing whitespace, they will have other problems than your failing script. Who can expect you to search the "@" key on your keyboard if the "*" key is so near?

Just use for-loops of the following form:

    # remove - remove files
    for file in $*
    do
	echo "$file does no longer contain whitespace"
	rm -f $file
    done

If somebody calls this remove script with the single argument "/etc/passwd Backup", he should have expected to lose some data.

If you had written the script in one of the following ways it would have worked:

    for file in "$@"
    do
	# ...
    done
or shorter:
    for file
    do
	# "$file" still contains whitespace...
    done

But why care? Just be sure to include a sentence like "in case of difficulties restore data from the backup" within your documentation.

Top of document


Use cat generously

Some people try to avoid the cat command as if it were something evil. They tell you that unnecessary cat invocations slow down script execution, increase memory usage, and complicate pipelines.

Don't believe them! Who cares for script execution speeds and unnecessary processes? cat is such a useful program! O.k., these people say

    cat file | grep searchstring

could be rewritten as

    grep searchstring < file

and every command of the following form

    cat onefile | prog1 | prog2
could be rewritten as
    prog1 < onefile | prog2
but y'know what?
cat onefile | cat | cat | cat | cat | cat | cat | prog1 | prog2

works as well!

You just can't do something wrong using cat, can you? This way you can even win an award!

Top of document


Use echo ""

Did you ever write DOS batch files? One annoying "feature" of the DOS "echo" command was, that the command line
    echo

did not result in an empty output line, but in the message "echo on" or "echo off". This obsolete nifty feature could be the reason why many UNIX shell scripts contain lines like

    echo ""
    echo "this line is preceeded by an empty line"
    echo ""

instead of

    echo
    echo "the previous line is empty"
    echo

Help preserving this last reminiscence of DOS in a windowing world!

Top of document

   
Copyright © 1998-2022 Heiner Steven (heiner.steven@shelldorado.com)