I want to display a welcome message in the start of my script:
echo "Running $0 $@"
But $@
might be very long. How can I break this line into multiple up-to-80 character lines?
I want to display a welcome message in the start of my script:
echo "Running $0 $@"
But $@
might be very long. How can I break this line into multiple up-to-80 character lines?
Use
fold
.-s
wraps on spaces.-w 80
sets width to 80 columns.Of course
fold
is the best option, but you can also achieve this withgrep
orsed
:Note, this will break at 80 chars + 1 word, so it might not be suitable for you.
Although others mentioned tools that break the lines just before the word that hits the 80-character limit, there is also another tool that tries to not break the lines just before end of after start of a sentence and does other things for better readability. Its usage is similar:
The
-w
option specifies the maximum width of the text, but the text will use only 93 percent of the line (this can be overridden by using-g
).This is how
fold
formats the text:And this is output from
fmt
:You can notice that the last (exactly 80-char long) get wrapped, but not at column 75 (the 93 % of 80), but a word before it, because we do not want to have one word on the line alone (in printed text etc.).