FIELD NOTES / UNIX & LINUXNew tools on weekdays. Beyond the man page.
$expand

expand: decide whether a tab is layout or data

Turn tabs into a stable human view without quietly rewriting the contract that a Makefile, parser, or evidence file depends on.

THE DEEP DIVE16 MIN READ
01

Start with the eight-column view

A tab is not a fixed run of spaces. It means advance to the next tab stop, so the number of spaces depends on the current column. expand reads each file, or standard input when no file is named, and writes a copy in which tab characters have been replaced by spaces. It does not modify the input file. The central question for this article is whether that copy is a useful human view or a damaging change to the data contract.

Work in a directory where you may create a new expand-demo subdirectory. mkdir creates it; each && runs the next command only after success; and cd enters it. printf interprets the trusted format: %s inserts each following quoted string, \t writes one tab byte, and \n ends the record. The shell's > redirection creates report.tsv, replacing that name if it exists. The printf article develops the difference between format and data.

With no options, expand uses tab stops every eight columns. In the output, ID occupies columns zero and one, so its tab becomes six spaces before OWNER begins at column eight. The six-character value 100025 needs only two spaces to reach the same stop. This is alignment calculated from position, not padding attached to a field.

TERMINALCommand — disposable lab
mkdir expand-demo &&
cd expand-demo &&
printf '%s\t%s\t%s\n' \
  'ID' 'OWNER' 'STATE' \
  '7' 'alex' 'open' \
  '100025' 'jo' 'closed' > report.tsv &&
expand report.tsv
TERMINALOutput
ID      OWNER   STATE
7       alex    open
100025  jo      closed

The result contains spaces, not tabs. Alignment is only a display convention: expand does not learn that these are fields, enforce a schema, or protect the original bytes if you redirect over another file.

02

Prove the conversion instead of trusting the screen

Terminals and editors can render a tab and several spaces identically. Inspect both streams as bytes when that distinction matters. od -A n suppresses address labels; -t x1 requests hexadecimal values in one-byte units; and -v prevents repeated rows from being abbreviated. The od article explains why a readable representation is evidence about bytes, not the bytes themselves.

The first command shows 09, the tab byte, after ID. The pipeline operator | connects expand's standard output directly to od's standard input. In the second view, that 09 has become six copies of 20, the space byte. The 0a byte ends the record.

TERMINALCommand
printf 'ID\tOWNER\n' | od -A n -t x1 -v
printf 'ID\tOWNER\n' | expand | od -A n -t x1 -v
TERMINALOutput bytes
 49 44 09 4f 57 4e 45 52 0a
 49 44 20 20 20 20 20 20 4f 57 4e 45 52 0a

expand removes tab bytes; it does not merely ask the terminal to display them differently. Keep the source artifact separately whenever byte identity, a signature, or a later parser matters.

03

Choose stops, and know where the list ends

The portable -t option takes a tab list. A single number is an interval: -t 4 places stops every four columns. A list such as -t 4,12,20 instead names specific increasing column positions. In the shell, the commas are ordinary characters inside one option argument; they do not run separate commands.

Here the explicit list puts OWNER at column four and STATE at column twelve. The third row behaves differently because 100025 has already passed column four: its first tab advances to column twelve. POSIX, GNU, FreeBSD, and OpenBSD agree that a tab beyond the final stop in a multiple-position list becomes one space. That rule is easy to miss when sample data never reaches the last stop.

TERMINALCommand
expand -t 4,12,20 report.tsv
printf '123456789012345678901\tX\n' | expand -t 4,12,20
TERMINALOutput
ID  OWNER   STATE
7   alex    open
100025      jo      closed
123456789012345678901 X

Use -t 4 when you mean a repeating four-column grid; use -t 4,12,20 only when those exact stops are the contract. Blank-separated lists are standardized too, but comma-separated lists avoid shell quoting and are clearer in scripts.

04

Offset a variable-width view deliberately

A one-character diff gutter shifts every later tab by one column. GNU's extension --tabs=1,+8 defines a stop at column one and then repeats every eight columns relative to that stop: one, nine, seventeen, and so on. --tabs is the long form of -t; the equals sign keeps its list in the same shell argument. The plus sign belongs to GNU's tab-list syntax, not shell arithmetic.

Both rows therefore align their first field at column nine even though key and longer have different widths. A final /8 is a different GNU extension: it continues at absolute multiples of eight after the explicit stops. Neither + nor / syntax, nor the long option name, belongs in a portable POSIX script.

TERMINALCommand — GNU extension
printf '+\tkey\tvalue\n-\tlonger\told\n' |
  expand --tabs=1,+8
TERMINALOutput
+        key     value
-        longer  old

This is a view-layer repair for a known one-column prefix. If prefixes can be wider than one display column, separate the prefix from the tabbed body or compute a tested layout; one fixed offset cannot correct arbitrary gutters.

05

Use initial-only mode for indentation

GNU -i, also spelled --initial, converts only tabs that occur before the first character that is neither a space nor a tab on each line. It is useful when leading indentation needs a stable view but tabs inside a record must remain visible to a later consumer. FreeBSD and OpenBSD expand do not document this option, and POSIX does not require it.

The fixture has one leading tab, then name, then an internal tab before value. -t 4 sets four-column stops. The leading tab becomes four spaces, but the internal tab remains byte 09. od uses the byte-view options introduced above so the preservation is explicit.

TERMINALCommand — GNU extension
printf '\tname\tvalue\n' |
  expand -i -t 4 |
  od -A n -t x1 -v
TERMINALOutput bytes
 20 20 20 20 6e 61 6d 65 09 76 61 6c 75 65 0a

Initial means before the first nonblank character, not merely the first byte on a line. This mode deliberately leaves part of the tab contract unchanged; document that mixed result before feeding it to another tool.

06

Backspace and Unicode change the column calculation

GNU, FreeBSD, and OpenBSD preserve a backspace byte and decrement the column count used for the next tab calculation. The first synthetic stream contains A, B, backspace, tab, X, and newline. After the backspace, the counter returns from column two to column one, so -t 4 emits three spaces. Backspace still remains byte 08; expand is not a terminal emulator and does not erase B.

Locale and implementation version matter for multibyte text. The second stream contains the UTF-8 character 界, a tab, and X. The installed GNU Coreutils 9.7 under C.UTF-8 counts the three UTF-8 bytes before the tab and emits one space. The byte dump records that local observation. GNU Coreutils 9.11 made expand and unexpand multibyte-character aware; 9.12, released September 14, 2026, is the current stable release at review time. On a current build, test the deployed locale and character widths rather than copying the 9.7 output.

LC_ALL=C.UTF-8 sets the locale for only the expand process to the left of the second pipeline. It does not convert the input encoding. A locale can inform character and display-width handling, but it cannot tell expand whether the file was actually encoded as claimed.

TERMINALCommand — GNU Coreutils 9.7 observations
printf 'AB\b\tX\n' | expand -t 4 | od -A n -t x1 -v
printf '界\tX\n' | LC_ALL=C.UTF-8 expand -t 4 |
  od -A n -t x1 -v
TERMINALOutput bytes
 41 42 08 20 20 20 58 0a
 e7 95 8c 20 58 0a

Do not infer visual safety from successful decoding. Combining marks, wide characters, backspaces, carriage returns, and terminal escape sequences can make rendered columns diverge from a simple byte view. Pin the implementation and test the characters your workflow accepts.

07

Normalize a visual diff, not the evidence

During a review, equivalent tab and space choices can obscure the substantive change. Create separate views of a trusted baseline and a supplied candidate, then compare those views. The first two printf commands create tiny fixtures. Each expand command writes a new .view file; && prevents the comparison if either conversion fails.

diff -u requests unified output. --label supplies stable header labels instead of timestamped filenames. The final -- ends diff's option parsing so the following names are operands. The diff article explains hunks and its special status 1 for differences. Here the heading's tab and four spaces normalize to the same bytes, leaving the role change visible.

TERMINALCommand — expected difference
printf 'name\trole\napi\treader\n' > baseline.txt &&
printf 'name    role\napi\twriter\n' > supplied.txt &&
expand -t 8 baseline.txt > baseline.view &&
expand -t 8 supplied.txt > supplied.view &&
diff -u --label baseline.view --label supplied.view -- \
  baseline.view supplied.view
TERMINALOutput; diff exits 1
--- baseline.view
+++ supplied.view
@@ -1,2 +1,2 @@
 name    role
-api     reader
+api     writer

This is a security-minded review aid, not sanitization. expand does not remove terminal escapes, validate an encoding, reject malicious content, or prove two originals equivalent. Inspect supplied text with a viewer that escapes control bytes, retain both originals, and never replace evidence with the normalized copies.

08

A Makefile tab is a contract, not decoration

Some formats assign meaning to a tab byte. A traditional Makefile recipe begins with a tab unless the file deliberately changes the recipe prefix. The fixture below is only a minimal sample: target and a colon, newline, a tab, command, and newline. It is not executed.

The first od view contains 09 before command. The second command pipes an expanded copy to od and shows eight 20 bytes instead. A human may prefer the aligned view, but saving it as the build input can change or invalidate the Makefile. The same warning applies to tab-separated data and any machine-readable format whose parser distinguishes tabs from spaces.

TERMINALCommand
printf 'target:\n\tcommand\n' > Makefile.sample
od -A n -t x1 -v Makefile.sample
expand -t 8 Makefile.sample | od -A n -t x1 -v
TERMINALInput bytes, then expanded bytes
 74 61 72 67 65 74 3a 0a 09 63 6f 6d 6d 61 6e 64
 0a
 74 61 72 67 65 74 3a 0a 20 20 20 20 20 20 20 20
 63 6f 6d 6d 61 6e 64 0a

Never run expand in place. It has no in-place option; a shell redirection such as expand file > file truncates file before expand reads it. Write a separate view, verify success, and use a deliberate replacement procedure only when changing the data is the stated goal.

09

Treat partial output as a failed result

With multiple operands, GNU 9.7 diagnoses an unreadable file, continues with a readable one, and exits nonzero. A consumer that captures only standard output can therefore receive a plausible but incomplete report. Run this demonstration in a subshell, the parentheses, so its status handling is contained.

The command redirects standard output to partial.txt and file descriptor 2, standard error, to expand.err. $? expands to the immediately preceding command's status; printf records it before cat displays the diagnostic and partial output. The shell quotes around $? prevent unwanted splitting, although this numeric value contains no spaces.

TERMINALCommand — expected failure
printf 'ok\trow\n' > readable.txt &&
(
  expand missing.txt readable.txt > partial.txt 2> expand.err
  printf 'status=%s\n' "$?"
  cat expand.err
  cat partial.txt
)
TERMINALOutput
status=1
expand: missing.txt: No such file or directory
ok      row

Diagnostic wording varies by implementation and locale. Automation should test the exit status, not match this English sentence. In a pipeline, enable and check an appropriate pipefail policy or capture component statuses; the last command's zero status does not prove expand succeeded upstream.

10

Write down the view contract

The portable intersection is intentionally small: input files or standard input, default eight-column stops, and -t with one interval or an increasing comma- or blank-separated list. GNU, FreeBSD, and OpenBSD preserve backspaces for output and column accounting. POSIX leaves implementation and locale details around character display to their documented contracts. GNU's -i and long options, its + and / continuation forms, and obsolete stop-list spellings are not a portable foundation.

Before using expand, state whether tabs are presentation or syntax; whether stops repeat or end; which locale and implementation define columns; whether control characters are allowed; and whether output is a disposable human view or new source data. The preceding fold article asks the same kind of question about inserted newlines: a display transformation is safe only when the transformed character is allowed at that boundary.

For routine administration, preserve the original, inspect ambiguous bytes, create the expanded view at the presentation edge, and reject any failed conversion. expand earns trust by making a chosen layout explicit. It becomes dangerous when a clean-looking screen is mistaken for an unchanged contract.

Back to latest issue
HELP SHAPE THE NEXT ISSUE

What belongs in your toolbox?

Choose a command you want to know better. We start with familiar tools and work toward the less obvious ones.

READER’S CHOICE

What should we cover next?

Vote for a future deep dive, or suggest a command we missed.

One command name, up to 32 characters. Suggestions are reviewed by a person. A new article starts a fresh poll.

Loading poll…