$_ CLI In Depth
Learning progress 0%
lesson performance tmux vim git

Building Your Own Toolkit

6 min read Module 18: Performance, Debugging & Power Tools Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:15

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.

BASH4 lines
#!/bin/bash
for f in "$@"; do
  printf "%s %s\n" "$(wc -l < "$f")" "$f"
done

This 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.

BASH4 lines
if [ "$#" -eq 0 ]; then
  echo "usage: tool FILE..." >&2
  exit 2
fi

The 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.

BASH2 lines
chmod +x wc-lines.sh
./wc-lines.sh notes.txt

The ./ 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.

Warning

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.

Terminal lab · 3 steps · real Linux in your browser

Build a small command-line tool

Write a script that counts lines, add usage and an exit status, and make it executable.

01
Write the first version
A tool is a script that takes arguments and prints a result. Write wc-lines.sh. For each file named on the command line it prints the line count and the name: ``bash cat > wc-lines.sh <<'EOF' #!/bin/bash for f in "$@"; do printf "%s %s\n" "$(wc -l < "$f")" "$f" done EOF `` Run it on any file, for example bash wc-lines.sh /etc/hostname, and check the output.
Show hint for step 1
wc -l < file prints only the count, without the file name. printf adds the name after it.
02
Add usage text and an exit status
A tool should explain itself when it is misused. If no file is given, print a usage line to standard error and exit with status 2. Add this check at the top, after the shebang line: ``bash if [ "$#" -eq 0 ]; then echo "usage: wc-lines.sh FILE..." >&2 exit 2 fi `` Run bash wc-lines.sh with no arguments, then run echo $? to see the status.
Show hint for step 2
Insert the block after line 1 with sed -i, or rewrite the whole file. Exit status 2 is the usual choice for a usage error.
03
Make the tool executable and run it
chmod +x sets the execute bit, so the script can run as ./wc-lines.sh. Create notes.txt with two lines, make the tool executable, and run it on the file: ``bash printf "one\ntwo\n" > notes.txt chmod +x wc-lines.sh ./wc-lines.sh notes.txt > tool-output.txt `` Open tool-output.txt. It should read 2 notes.txt.
Show hint for step 3
The first line of the script says which interpreter runs it, so the execute bit is enough. ./ runs the file in the current directory.
Check yourself Question 1 of 4
Concept Check Usage error status recall

Which exit status does this lesson use for a usage error?

Recap & Key Takeaways

A script takes arguments from $1 and $@, and quoting "$@" keeps each argument intact.
Print usage to standard error and exit with status 2 for misuse, so that callers can rely on the status.
chmod +x makes a script executable, and ./name runs a script in the current directory without PATH.
set -euo pipefail after the shebang stops most scripts at the first silent failure.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue