# The Binaries

## Introduction

Laravel Bash is mostly other people's programs. Every feature is one of the channels Unix gives every process, and a program that knows how to use it. This page lists them, and why we picked each one over the alternatives.

## How Programs Talk

Every Unix process gets the same handful of ways to communicate, and every Laravel concept turns out to be one of them:

| Channel | What it carries | Laravel Bash uses it for |
|---|---|---|
| Arguments | Input, once, at the start | Job arguments, route parameters |
| Environment variables | Configuration, inherited by children | `.env`, `DB_CONNECTION` |
| stdin / stdout | A stream of text | Requests, responses, Eloquent pipelines |
| stderr | Diagnostics | Stack traces, the log |
| Exit codes | One number, 0 to 255 | Exceptions, validation, failed jobs |
| Signals | A doorbell with no message | Events, stopping workers |
| Files and renames | Anything, durably | Queues, the event log, broadcasts |
| Sockets | Bytes between machines | HTTP |

## The Binaries

| Binary | Job in Laravel Bash | Why this one |
|---|---|---|
| `socat` | The web server | `nc` has three incompatible versions (OpenBSD, GNU and BusyBox), and Bash's `/dev/tcp` can connect but can't listen |
| `sqlite3`, `psql` | The database drivers | Both print tab-separated text with `-separator` and `--field-separator` |
| `awk` | `where`, `only`, the Markdown renderer | A whole language for columns of text, in every POSIX system |
| `sort` | `orderBy` | `-t` and `-k` sort by a field; `-g` sorts numbers; `-s` keeps ties in order |
| `head` | `take` | It exits early, and SIGPIPE stops everything upstream |
| `cut` | `pluck` | The fastest way to take one column |
| `join` | `withCount` | A real relational join, on sorted text |
| `uniq -c` | Counting relations | `GROUP BY` and `COUNT(*)` in one flag |
| `column -t` | Tinker's tables | Lines everything up |
| `jq` | JSON | Quoting JSON by hand is how security advisories are written |
| `mv` | Claiming jobs | A rename is atomic on one filesystem |
| `timeout` | Job timeouts | Exits with 124, so we know why a job died |
| `flock` | `withoutOverlapping` | `-n` takes the lock or gives up at once |
| `tail -F` | Broadcasting | Follows a file, even after it's rotated |
| `grep --line-buffered` | Filtering broadcasts | Without it, events arrive late in 4 KB lumps |
| `pkill`, `pgrep` | Events, Horizon | Find processes by their command line |
| `mktemp` | Response bodies, concurrency results | Unique files nobody else will write to |
| `sed` | Migrations, stripping colours | Search and replace in a stream |
| `shellcheck`, `bats` | The test suite | Larastan and Pest, respectively |

## Builtins Before Binaries

Starting a program is the expensive part of shell scripting: a `fork` and an `exec` cost far more than anything Bash does on its own. So wherever Bash has a builtin that does the job, Laravel Bash uses it:

- `printf '%(%F %T)T'` instead of `date`, for log timestamps.
- `${#value}` and `[[ =~ ]]` instead of `wc` and `grep`, for [validation](/docs/validation).
- `${s//</&lt;}` instead of `sed`, for escaping HTML.
- `$EPOCHREALTIME` instead of `date +%s%N`, for timing requests.
- `read -N` instead of `head -c`, for request bodies.

> [!NOTE]
> The rule is: use a builtin when one value is involved, and a program when a stream is. `sed` beats a Bash loop over 10,000 lines. A Bash substitution beats `sed` on one string.
