Americas

  • United States
sandra_henrystocker
Unix Dweeb

The Linux fold command breaks up text, drives loops

How-To
Jul 01, 20224 mins
Linux

The Linux fold command can break long lines of text into pieces, but it can also be used to create arguments for looping in scripts.

The Linux fold command enables you to break a string of characters into same-size chunks, but it can also be used to provide a series of characters or strings to drive a loop. This post reviews the basic command and then demonstrates how you can use it to loop through the characters or strings that it creates.

The basic use of the fold command is to take long lines of text and break them into shorter pieces. One common use is to shorten lines in a text file so that they display well in a terminal window. Lines wider than the terminal width might otherwise wrap in inconvenient places.

The fold command can also be used to create a narrower file from a file with lines that are inconveniently long.

$ fold wide.txt > narrow.txt

If a text file contained a single line with 346 characters including the final newline like that shown below, it would wrap, but not necessarily in a way that would look right.

$ cat shs
Sandra Henry-Stocker has been administering Unix systems for more than 30 years.
 She describes herself as “USL” (Unix as a second language) but remembers enough       
 English to write books and buy groceries. She lives in the mountains in Virgini
a where, when not working with or writing about Unix, she’s chasing the bears aw
ay from her bird feeders.

If you were to use the fold command with no options, it would look the same—breaking by default at the end of each 80-character chunk even if that position were in the middle of a word.

$ fold shs
Sandra Henry-Stocker has been administering Unix systems for more than 30 years.
 She describes herself as “USL” (Unix as a second language) but remembers enough       
 English to write books and buy groceries. She lives in the mountains in Virgini
a where, when not working with or writing about Unix, she’s chasing the bears aw ay from her bird feeders.

However, if we add the -s (space) option, the file would be folded on the last blank in each 80-character chunk, so the displayed file would look as it should.

$ fold -s shs
Sandra Henry-Stocker has been administering Unix systems for more than 30
years. She describes herself as “USL” (Unix as a second language) but remembers
enough English to write books and buy groceries. She lives in the mountains in
Virginia where, when not working with or writing about Unix, she’s chasing the
bears away from her bird feeders.

Note that the fold command defaults to 80 characters even if your terminal window is narrower or wider. If you use the -w (width) option along with the -s (space) option, you can display the text properly but in a more narrow display.

$ fold -w 40 -s shs
Sandra Henry-Stocker has been
administering Unix systems for more
than 30 years. She describes herself as
“USL” (Unix as a second language) but
remembers enough English to write books
and buy groceries. She lives in the
mountains in Virginia where, when not
working with or writing about Unix,
she’s chasing the bears away from her
bird feeders.

Strings can be broken into as few letters as you like. If you wanted to break the alphabet into 7-character chunks, you could do something like this:

$ echo abcdefghijklmnopqrstuvwxyz | fold -c7
abcdefg
hijklmn
opqrstu
vwxyz

If you want to loop through a sequence of characters in order to do something with each of them, you can use the fold command to process the characters one at a time. The script below runs through primary vowels (not “y”) in both upper- and lowercase and removes them one at a time from whatever string is entered by the user.

#!/bin/bash

echo -n “Enter a phrase> “
read phrase

for letter in `echo AEIOUaeiou | fold -w1`
do
    phrase=`echo $phrase | sed “s/$letter//g”`
done

echo $phrase

Here’s an example of how it runs:

$ rmVowels
Enter a phrase> Every day is another chance to start over
vry dy s nthr chnc t strt vr

If you want to loop through the days of the week, you could do something like this:

$ for day in `echo SunMonTueWedThuFriSat | fold -w3`
do
    echo $day
done
Sun
Mon
Tue
Wed
Thu
Fri
Sat

Wrap-Up

Any time you need to loop through a series of values that is not easily derived from other Linux commands, you might consider using a cleverly crafted fold command.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author