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 examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe