Posts

Showing posts from July, 2022

How to use Linux fold command in bash script

The fold command can be very useful in many scenarios. It allows you to break up text and then use it in a loop. In this post, we will review the fold command and see how it can be used in a bash script and as command argument to format text. 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 formatted 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 khs Kendra Harry-Stocker has been administering Unix systems for more than 30 years. She describes herself as “USL” (Unix as a second la...