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. Credit: tine ivanic 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. Related content how-to How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe