Description:
Learn how to effectively format long command-line messages by breaking them into 80 character lines for better readability.
---
This video is based on the question https://askubuntu.com/q/1398716/ asked by the user 'CIsForCookies' ( https://askubuntu.com/u/768410/ ) and on the answer https://askubuntu.com/a/1398719/ provided by the user 'Artur Meinild' ( https://askubuntu.com/u/1066942/ ) at 'Ask Ubuntu' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: How to break string into N 80 character lines?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.
If anything seems off to you, please feel free to drop me a comment under this video.
---
How to Break a String into N 80 Character Lines
When working with scripts, especially in command-line interfaces, you may find yourself needing to display long messages or outputs. One common requirement is to break such messages into manageable chunks, particularly if they exceed a certain width. In this guide, we will explore a simple and efficient way to format your command-line outputs by breaking them into lines of up to 80 characters.
The Problem: Long Command-Line Outputs
Imagine you want to display a welcome message at the start of your script. For instance:
[[See Video to Reveal this Text or Code Snippet]]
Here, $@ represents all the arguments passed to the script. However, if these arguments are extensive, the output can overflow the terminal window, making it hard to read. Thus, our goal is to break this output into multiple lines, each containing no more than 80 characters.
The Solution: Using the fold Command
A straightforward solution to handle long strings in the command line is to use the fold command. This utility allows you to format text output by wrapping it to fit within a specified width.
How to Implement the Solution
To display your welcome message and ensure it adheres to the 80-character limit, you would modify the command like this:
[[See Video to Reveal this Text or Code Snippet]]
Breaking Down the Command
echo "Running $0 $@ ": This command constructs your message. Here, $0 is the name of the script being executed, and $@ includes all script arguments.
|: The pipe symbol takes the output of the echo command and passes it as input to the fold command.
fold -sw 80: This command transforms the output text to fit within 80 characters.
-s: This option ensures that the lines are wrapped at spaces, keeping words intact rather than cutting them off in the middle.
-w 80: This specifies the maximum width of each line, in this case, 80 characters.
Benefits of Using fold
Improved Readability: By breaking long lines into shorter segments, the output becomes easier to read.
Preserved Formatting: Using the -s option maintains word integrity, making sure words are not split across lines.
Simplicity: The method is straightforward, requiring only a single command to achieve the desired result.
Conclusion
Being able to format command-line outputs effectively is crucial for creating user-friendly scripts. Utilizing the fold command allows you to ensure that your messages remain within a readable limit, enhancing user experience.
So, next time you need to display a lengthy output in your scripts, remember this simple trick to keep your command-line interfaces neat and organized. Happy scripting!
Share this link via
Or copy link


![Why Using [dbo].[table_name] is Better Than dbo.table_name in SQL Server](https://i.ytimg.com/vi/iGb7D4YyQiE/maxresdefault.jpg)


























