Previous Table of Contents Next

Module 46
expand/unexpand (BSD)


The external expand command expands tabs to spaces. The expand command reads the named files or the standard input, changes the tabs to the appropriate number of blanks, and writes the results on the standard output.

The external unexpand command replaces leading spaces and tabs with just tabs where possible.


Following is the general format of the expand command.

     expand   [ -tabstop | -tab1,tab2,tab3,...,tabn ] [ file_list ]
     unexpand [ -a ] [ file_list ]


The following options may be used to control how expand functions.

-tabstop Sets tab stops tabstop spaces apart.
-tab1,.,tabn Sets tabs at specific tab positions defined by tab1 through tabn.
-a Tabs are inserted at all occurrences that would replace two or more spaces. Without the -a option unexpand only replaces leading blanks and tabs.


The following argument may be passed to the expand command.

file_list The list of files read and processed by expand. If no files are specified expand reads the standard input.


Refer to the tabs command described in Module 130.


The expand command reads from standard input or files and writes to standard output.


You use the expand command to convert tab characters to the appropriate number of spaces. You may find expand useful to preprocess character files that contain tabs before performing character processing on the files. For example, you may want to remove tabs before sorting or examining specific columns of a file.


In this activity you use the expand command to remove tabs from a file containing tab-separated columns. Begin at the shell prompt.

1.  Type cat -v -t file1 and press Return. Notice the ^I (tabs) embedded in the output.
2.  Type expand file1 | cat -v -t and press Return. Notice the output has no tabs in it.
3.  Turn to Module 143 to continue the learning sequence.

Previous Table of Contents Next