Start with a small script
A script is a text file that bash runs line by line. Its first line, called the shebang, names the interpreter, such as /bin/bash. Arguments arrive in numbered parameters: $1 is the first argument, and $@ holds all of them. The count of arguments is in $#. A script that does one thing well is easier to trust than one that tries to do everything, so start with a single task and grow the script only when you need to.
#!/bin/bash
for f in "$@"; do
printf "%s %s\n" "$(wc -l < "$f")" "$f"
doneThis tool counts the lines in each file it is given. Quoting "$@" keeps each argument intact, even when a file name has spaces, which is the detail that breaks most first versions.
Usage and exit status
A tool should handle misuse politely. If it is run with no arguments, print a usage line to standard error, not standard output, so that a pipeline does not receive the message as data. Then exit with a non-zero status. The convention is status 2 for a usage error and 1 for other failures, and 0 only when the job is done. Scripts that call your tool depend on that status, so it matters as much as the output.
if [ "$#" -eq 0 ]; then
echo "usage: tool FILE..." >&2
exit 2
fiThe test command compares the argument count, and the redirection sends the message to standard error. The exit statement sets the status of the whole script.
Make it executable and findable
A script becomes a command when it has the execute bit and it lives in a directory on PATH. chmod +x sets the bit. Running ./tool executes a script in the current directory, and a script in a directory on PATH runs by name from anywhere. A common home for personal tools is a bin directory in your home folder, added to PATH in a startup file.
chmod +x wc-lines.sh
./wc-lines.sh notes.txtThe ./ prefix matters. Without it, bash searches PATH for the name, and a script in the current directory is not on PATH by default. The prefix tells bash exactly which file to run.
Strict mode
Strict mode makes a script stop when something goes wrong. set -e exits on the first failed command, set -u treats an unset variable as an error, and set -o pipefail makes a pipeline fail when any stage fails. Together they are written set -euo pipefail, and placing that line after the shebang protects every later line. Strict mode is not a complete safety net, because a failed command inside a condition does not stop the script, but it catches most silent failures.
set -e does not stop a script when a failing command is the left side of an && or || list, or when it is inside an if condition. Check such commands yourself when the result matters.
Options with getopts
When a tool needs flags, the built-in getopts command parses them in a loop. It reads options such as -o with a value, sets a variable for each one, and leaves the remaining arguments in place. The GNU and POSIX conventions for option names are described in the utility guidelines, and following them makes a tool feel familiar to anyone who uses the standard utilities.
Temporary files and cleanup
A tool that needs a scratch file should create it with mktemp, which picks a unique name in the temporary directory. A trap on EXIT removes the file when the script ends, even after an error. That pattern keeps the temporary directory clean, and it avoids clashes when two copies of the tool run at once.
Keep the toolkit small, test each tool on the lab machine, and read the usage text of the standard utilities for the flags you copy. The next lesson applies the same habits to a health report.
Where tools belong
A personal tool is easiest to use when it lives in a directory on PATH, such as a bin folder in your home directory. The type command with the -a option shows where a name resolves, and command -v prints the path of a command, which is useful for checking that a new tool is found before the old one. If a name is already taken by a builtin or an alias, the new script is shadowed, so check the lookup order before you wonder why a change has no effect.
Testing a tool before you trust it
Test a tool with small inputs whose answers you know, such as a file with three lines, and an empty file. Test the usage path and the exit status along with the output. Run bash -n on the script after each change, which catches a missing quote or an unbalanced block before any command runs. Keep the test inputs in a folder next to the script, so that a later change can be checked against the same cases.
Documenting usage in the script
A comment block at the top of the script should say what the tool does, what it takes, and what it returns. The usage line printed on error should match the comment, so that the two never disagree. A short example in the comment is often the fastest way for a future reader to understand the tool, and it doubles as a test case.
Good tools share a few habits. They read from standard input when no file is given, so that they fit in a pipeline. They write results to standard output and errors to standard error, so their output can be captured. They exit with a clear status, and they do nothing destructive without an explicit flag. A tool that follows these habits can be combined with the standard utilities and tested like any of them.
When a tool grows past a few screens, split the shared steps into functions defined at the top of the script, and keep the main flow short. The function names then read as a table of contents, which is easier to maintain than one long block.