Overview
top shows a live table of the processes that use the most processor time or memory, with a summary of load and memory above it. It refreshes every few seconds until you press q. It is meant for a person at a terminal. For scripts, each version has a batch form that prints once and exits, which is what the examples use.
Background
Today the name covers top from procps-ng, the top that macOS ships and the top applet in BusyBox. They differ widely in their options: macOS uses -l for a number of samples, and procps and BusyBox use -b -n. The first example on this page needs a terminal and was reviewed only.
Examples
Run it at a terminal; keys such as q (quit) and k (kill) are shown in the top lines:
top
Common mistakes
top takes a few seconds to sample, so a script that calls it is slow. The first sample can show wrong values for the percentage of processor, because it is an average since the process began. Press q to quit; if the terminal is left in a strange state, run reset. Do not parse its screen output: ps -o gives columns that scripts can read.
Portability
The example that needs a terminal was reviewed only. The batch forms differ: macOS top -l 1, procps and BusyBox top -b -n 1. Their first line differs too, as the sections below show. The batch examples in the sections below were executed on macOS system tools, procps-ng and BusyBox 1.37.0.
References
Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.