Bash/General Use

From Linux Hints

Jump to: navigation, search

Contents

General Use

To get the best use of Bash, you should read the man page. It's extensive, and gives you an idea of what you can do with it; although it lacks examples, which sometimes leave you guessing.

Bash Error Handling

In almost all cases (with some notable exceptions) I recommend the use of the -e flag when running bash scripts. You can do this in a number of ways. At the start of a script:

#!/bin/bash -e

In the script:

set -e

When running manually:

bash -e ./myscript

This means that the script will terminate as soon as it sees an error. This can be particularly important, not just as a general programming principle, but it means for example if you misparse a filename due to a syntax error, you don't end up passing '/' to rm -rf as root.

Debugging

Use the -x flag for debugging of a script; this traces its execution. You might want to use this:

bash -ex ./myscript 2>&1 | less

See Also

related