← All commands
THE COMMAND REFERENCE

expand

Convert tabs to spaces.

Last updated

GNU

Implementation-level reference; examples are not certified for every installed version.

Overview

expand replaces each tab character with enough spaces to reach the next tab stop, which is every 8 columns by default. Use it when text with tabs must look the same everywhere, for example in a plain-text report or when pasting into a program that treats tabs strangely. The reverse is unexpand.

Background

The expand utility first appeared in 1BSD, according to the macOS manual, which also says expand and unexpand conform to POSIX.1-2001. Today the name covers GNU expand, the expand that macOS ships, and the expand applet in BusyBox. They agree on -t for the tab width.

Examples

Convert a tab to spaces with the default width of 8 columns:

printf 'a\tb\n' | expand

Use a tab stop every 4 columns with -t:

printf 'a\tb\n' | expand -t 4

Check that no tab is left, by looking for the \t that od -c prints for a tab:

printf 'a\tb\n' | expand | od -c | grep -q '\\t' || echo no tabs left

Convert a file, writing the result to a new one:

printf 'x\ty\n' > in.txt && expand in.txt > out.txt && cat out.txt

Options

-t
Set the distance between tab stops, in columns, as in -t 4.

-i, --initial
Convert only the tabs at the start of a line.

Common mistakes

A tab moves to the next tab stop, not a fixed number of spaces, so a<TAB>b and abcdefg<TAB>b produce different amounts of space. expand reads a file and writes to standard output; it does not change the file in place. In a Makefile a tab is required, so never run expand on one.

Portability

Only -t with one number is in the Common examples. GNU also accepts a list of stops and -i to change only leading tabs, which is not in every implementation. The Common examples were executed on GNU coreutils 9.11, macOS system tools, and BusyBox 1.37.0 (see the development guide for the exact check).

About GNU expand

GNU expand is part of GNU coreutils, and this page's checks ran coreutils 9.11.

GNU examples

Change only the tabs at the start of each line with -i, leaving later tabs alone:

printf '\ta\tb\n' | expand -i -t 4 | od -c | grep -c '\\t'

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.