The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group


fold - filter for folding lines


fold [-bs][-w width][file...]


The fold utility is a filter that will fold lines from its input files, breaking the lines to have a maximum of width column positions (or bytes, if the -b option is specified). Lines will be broken by the insertion of a newline character such that each output line (referred to later in this section as a segment) is the maximum width possible that does not exceed the specified number of column positions (or bytes). A line will not be broken in the middle of a character. The behaviour is undefined if width is less than the number of columns any single character in the input would occupy.

If the carriage-return, backspace or tab characters are encountered in the input, and the -b option is not specified, they will be treated specially:

The current count of line width will be decremented by one, although the count never will become negative. The fold utility will not insert a newline character immediately before or after any backspace character.
The current count of line width will be set to zero. The fold utility will not insert a newline character immediately before or after any carriage-return character.
Each tab character encountered will advance the column position pointer to the next tab stop. Tab stops will be at each column position n such that n modulo 8 equals 1.


The fold utility supports the XBD specification, Utility Syntax Guidelines  .

The following options are supported:

Count width in bytes rather than column positions.
If a segment of a line contains a blank character within the first width column positions (or bytes), break the line after the last such blank character meeting the width constraints. If there is no blank character meeting the requirements, the -s option will have no effect for that output segment of the input line.
-w width
Specify the maximum line length, in column positions (or bytes if -b is specified). The results are unspecified if width is not a positive decimal number. The default value is 80.


The following operand is supported:
A pathname of a text file to be folded. If no file operands are specified, the standard input will be used.


The standard input will be used only if no file operands are specified. See the INPUT FILES section.


If the -b option is specified, the input files must be text files except that the lines are not limited to {LINE_MAX} bytes in length. If the -b option is not specified, the input files must be text files.


The following environment variables affect the execution of fold:
Provide a default value for the internationalisation variables that are unset or null. If LANG is unset or null, the corresponding value from the implementation-dependent default locale will be used. If any of the internationalisation variables contains an invalid setting, the utility will behave as if none of the variables had been defined.
If set to a non-empty string value, override the values of all the other internationalisation variables.
Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), and for the determination of the width in column positions each character would occupy on a constant-width font output device.
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.
Determine the location of message catalogues for the processing of LC_MESSAGES .




The standard output will be a file containing a sequence of characters whose order will be preserved from the input files, possibly with inserted newline characters.


Used only for diagnostic messages.






The following exit values are returned:
All input files were processed successfully.
An error occurred.




The cut and fold utilities can be used to create text files out of files with arbitrary line lengths. The cut utility should be used when the number of lines (or records) needs to remain constant. The fold utility should be used when the contents of long lines need to be kept contiguous.

The fold utility is frequently used to send text files to printers that truncate, rather than fold, lines wider than the printer is able to print (usually 80 or 132 column positions).


An example invocation that submits a file of possibly long lines to the printer (under the assumption that the user knows the line width of the printer to be assigned by lp):

fold -w 132 bigfile | lp





UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]