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

paste: assemble rows without inventing relationships

Combine ordered records, reshape a stream, and preserve the boundary between positional assembly and matching by identity.

THE DEEP DIVE14 MIN READ
01

Put corresponding lines beside each other

An inventory collector has produced two ordered lists: hostnames and the modes of their configuration files. You want a compact report. paste can place line 1 of one list beside line 1 of the other, then do the same for line 2. It does not look up a hostname, compare a key, or decide whether the values belong together. That distinction is the useful part of learning this small command.

The examples use GNU Coreutils 9.4 and Bash 5.2.21 on Linux. The ordinary parallel operation, -d delimiter option, -s serial flag, and explicit standard-input operand are specified by POSIX.1-2024. The later -z example is a GNU extension. All delimiters used here are ASCII; no multibyte-delimiter behavior is assumed.

Start in a disposable directory of your choosing. This setup creates a new paste-demo subdirectory and two synthetic files; it neither reads nor changes real system permissions. mkdir creates the directory, and each && runs the next command only if the previous command succeeded. If paste-demo already exists, stop and choose another empty parent directory rather than continuing with old fixtures. Later examples explicitly reuse these files.

printf's '%s\n' format prints each following string and a newline; single quotes preserve the format for printf to interpret. The shell's > writes that output to the named file. The printf article develops the format/data distinction. Finally, paste reads the two files and writes to standard output, normally the terminal, without modifying either input. Its default separator is a tab.

TERMINALCommand
mkdir paste-demo &&
printf '%s\n' edge01 edge02 edge03 > paste-demo/hosts &&
printf '%s\n' 0600 0644 0600 > paste-demo/modes &&
paste paste-demo/hosts paste-demo/modes
TERMINALOutput (one tab between hostname and mode)
edge01	0600
edge02	0644
edge03	0600

The relationship comes from the collector's ordering contract, not from paste. We will use that report to explore what happens when a value is absent, a list moves, or a boundary means something different.

02

A short file leaves an empty position

Suppose the mode list ends after edge02. Create short-modes below; the redirect creates or overwrites only that demonstration file. The -d option replaces the default tab with a delimiter from its argument. Here the argument is a single vertical bar. Single quotes prevent the shell from treating that bar as a pipeline operator.

POSIX specifies that, in parallel mode, a file that reaches end-of-file contributes empty lines while another file still has data. That makes a short input different from an error. Run the following to make the empty final position visible.

TERMINALCommand
printf '%s\n' 0600 0644 > paste-demo/short-modes &&
paste -d '|' paste-demo/hosts paste-demo/short-modes
TERMINALOutput
edge01|0600
edge02|0644
edge03|

The command succeeds. The trailing bar means there is an empty second position, not that paste established why it was empty. An actual blank third line in short-modes would produce the same display; an empty file would contribute an empty position on every row. A later value cannot recover a missing middle row's identity: if a collector omits that row instead of writing an explicit blank, all subsequent associations shift.

03

A missing file is a different failure

The new fixture directory has no file named missing. Try to read it to distinguish a nonexistent input from an existing input that runs out. LC_ALL=C is a shell environment assignment applied only to this paste process; it selects the C locale so the diagnostic below is reproducible in English.

TERMINALCommand
LC_ALL=C paste paste-demo/hosts paste-demo/missing
TERMINALStandard error (no standard output)
paste: paste-demo/missing: No such file or directory

GNU 9.4 exits with status 1 here. POSIX requires a nonzero status for an error and no standard output when a parallel-mode input cannot be opened. Do not generalize that no-output guarantee to every possible read or write failure, or to serial mode: output may already have been produced. A shell redirect into an output file also happens before paste runs, so a failure can still leave a created or truncated destination. Preserve the original artifact and check completion before accepting a report.

04

Equal lengths can still produce a false report

Now write the same three modes in a different order. This creates or overwrites reordered-modes; hosts and modes remain unchanged. The resulting report has three complete-looking rows and paste finishes successfully.

TERMINALCommand
printf '%s\n' 0644 0600 0600 > paste-demo/reordered-modes &&
paste -d '|' paste-demo/hosts paste-demo/reordered-modes
TERMINALOutput
edge01|0644
edge02|0600
edge03|0600

The mode now attributed to edge01 belongs to edge02 in our original fixture. In an authorized configuration review, 0600 versus 0644 can matter: these ordinary octal file modes differ in whether group and other users have read permission. paste has not inspected a file, proved exposure, or discovered a vulnerability. It has assembled supplied strings. A row-count check can detect some losses, but equal counts do not establish identity or ordering. Keep the hostname attached at collection time; independently sorting the two columns does not repair the lost relationship.

05

A delimiter list is a cycle, not a separator string

For a side-by-side diagnostic, place the original host/mode pair next to the host/reordered-mode pair. Repeating the hosts filename opens another reading of that file; it does not consume the next hostname from the first reading.

The argument '|:' supplies two delimiters: bar, then colon. paste uses them at successive boundaries between inputs, cycling back to the bar for the third boundary. In parallel mode that cycle restarts at the beginning of each output row. It does not insert the two-character string |: at every boundary.

TERMINALCommand
paste -d '|:' paste-demo/hosts paste-demo/modes paste-demo/hosts paste-demo/reordered-modes
TERMINALOutput
edge01|0600:edge01|0644
edge02|0644:edge02|0600
edge03|0600:edge03|0600

This is a display of two positional assemblies, not a comparison operation. The repeated hostname makes the layout easier to read but does not validate the second mode. Missing positions still consume their delimiter boundaries. Use one delimiter when downstream software expects a uniform field separator; paste does not add escaping when a field already contains it.

06

An empty delimiter does not write a zero byte

Sometimes independently produced fragments genuinely belong directly beside each other. The POSIX delimiter escape \0 means insert nothing at that boundary. It does not mean the NUL character, which is a zero-valued byte. Here we deliberately remove the boundary from the original display to see what information disappears.

Single quotes pass the backslash and zero intact to paste, which interprets the escape. They do not ask the shell to create a zero byte. The final newline that ends each output row remains.

TERMINALCommand
paste -d '\0' paste-demo/hosts paste-demo/modes
TERMINALOutput
edge010600
edge020644
edge030600

This is appropriate for a documented fixed-layout reconstruction, not for inventing a parseable hostname/mode format. An empty element still occupies its place in a longer delimiter cycle. POSIX also defines \t for tab, \n for newline, and \\ for a literal backslash in this option's argument. Use documented escapes and quote them; arbitrary backslash sequences do not have a portable meaning.

07

Serial mode makes one summary row per file

The reporting question can change from “which mode belongs beside each host?” to “what values did each column contain, in order?” The -s flag selects serial mode: finish one file before processing the next. Combined with -d ',', it replaces the internal line boundaries of each file with commas, leaving a final newline after that file's summary.

TERMINALCommand
paste -s -d ',' paste-demo/hosts paste-demo/modes
TERMINALOutput
edge01,edge02,edge03
0600,0644,0600

These rows summarize separate files; they are no longer host/mode records. With a longer delimiter list, the cycle resets for each file. An empty input file contributes an empty output line in serial mode. This is useful for a compact report of controlled tokens, but it is not a CSV serializer: embedded commas, quotes, and newlines are not quoted or escaped for CSV readers.

08

Repeated standard input means successive records

A producer may emit one ordered stream rather than separate files. The operand - tells paste to read standard input. Repeating - means taking another line from that same stream in turn, not making a copy of it. POSIX specifies this circular reading order. Use explicit - operands for portability; GNU also accepts standard input when no files are named.

The pipe sends printf's five synthetic hostnames into paste. With two - operands, each output row consumes two input lines. This groups an already ordered stream for display; it does not pair hosts with independently obtained measurements.

TERMINALCommand
printf '%s\n' edge01 edge02 edge03 edge04 edge05 | paste -d '|' - -
TERMINALOutput
edge01|edge02
edge03|edge04
edge05|

The final bar is the same empty-position rule we saw with a short file. Replacing the pipe with shell input redirection, paste -d '|' - - < paste-demo/hosts, reads the existing hosts file through standard input and produces edge01|edge02, then edge03|. The < operator opens a file as the command's standard input; it does not introduce another paste file operand. By contrast, naming hosts twice would pair each hostname with itself.

09

Serial delimiters can place the line breaks

There is another way to arrange that five-item stream into pairs. In serial mode the delimiter list can alternate a bar with an actual newline. The shell preserves '|\n' as one argument; paste turns its \n escape into a line break. A single explicit - tells it which input to serialize.

TERMINALCommand
printf '%s\n' edge01 edge02 edge03 edge04 edge05 | paste -s -d '|\n' -
TERMINALOutput
edge01|edge02
edge03|edge04
edge05

The output resembles the previous grouping but has no trailing empty field. Serial mode replaces boundaries between existing input lines; it does not pad a second input position. Its final newline is retained separately. A delimiter can therefore create physical output lines inside one serial operation. Choose between these forms according to the downstream format's incomplete-row rule, not merely their appearance on an even-sized example.

10

NUL changes record boundaries, not the field grammar

For a supplied pathname inventory, newline may occur inside a name. GNU -z changes both input and output record terminators to NUL, the zero byte. Unlike the empty delimiter escape \0, it is a record-boundary choice. The GNU manual documents this extension; it is not part of the POSIX paste interface described above.

This self-contained stream contains two synthetic names, cfg/edge followed by an embedded newline and 01, then cfg/edge02. No paths are accessed. printf interprets \n and \000 in its format as newline and NUL. The two - operands take successive NUL-terminated records, while -d '|' still inserts a bar between them.

The final pipe feeds the result to od for byte inspection. As in the od article, -An suppresses address offsets, and -t x1 selects one-byte hexadecimal values. That display makes embedded newline 0a, bar 7c, and terminating NUL 00 distinguishable.

TERMINALCommand
printf 'cfg/edge\n01\000cfg/edge02\000' | paste -z -d '|' - - | od -An -t x1
TERMINALOutput
 63 66 67 2f 65 64 67 65 0a 30 31 7c 63 66 67 2f
 65 64 67 65 30 32 00

The embedded newline survives inside the combined record; the terminating zero byte ends that record. This controlled format works because the two supplied names contain no bar. Arbitrary Unix filenames can contain bars and tabs, so NUL-framing the outer record does not make an unescaped inner field separator unambiguous. Keep every receiving stage NUL-aware and define the field grammar separately. This is not a general serializer for arbitrary filename tuples, nor a validation of the names.

11

Keep identity in the record when it matters

The safest repair for our false permission report is upstream: retain the hostname and mode in each source record. Then use a record-aware tool to choose a display. This synthetic three-line input keeps each association intact. awk's default field splitting treats runs of spaces and tabs as separators; these controlled values contain neither internally.

The awk article explains its per-record program model. Here the single quotes protect the program from the shell; braces enclose the action. $1 and $2 are the first and second fields. print emits those fields with the literal bar string between them; adjacent expressions concatenate, and print ends the result with a newline. No sorting or lookup is required because the relationship never left its record.

TERMINALCommand
printf '%s\n' 'edge01 0600' 'edge02 0644' 'edge03 0600' | awk '{print $1 "|" $2}'
TERMINALOutput
edge01|0600
edge02|0644
edge03|0600

When independent files both retain a common identifier, join matches fields rather than row numbers. Its ordinary inputs must be sorted compatibly on the chosen join field; duplicate keys can produce multiple matching pairs, and unmatched records need an explicit policy. The GNU join manual covers those choices. Use awk for record selection, validation rules, or calculations, and a real format parser when quoting changes field boundaries. Use paste when positional correspondence is already justified. It can assemble that relationship efficiently; it cannot establish it.

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…