Keeping scripts flexible

Allow your script's users to use the tool in whatever way they may need. For example, avoid hardcoding filenames to be read in your scripts, if you can; instead, allow the user to provide filenames as options or arguments.

If your script works on files, consider making it work the same way the classic Unix text filtering tools do. If there are no arguments, read any data needed from the standard input:

$ myscript

If there are arguments, read the data from (or make changes to) all of those files, in order:

$ myscript file1 file2

If one of the arguments is a dash, read from the standard input at that point in the argument loop, to allow us to provide a prefix and a suffix to the data:

$ myscript file1 - file2

These ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.