I have a scenario where I need to convert a long string of text and break it by words not exceeding a set line length (70 characters). I need to replace the space with a '\n' where the line break would be.
Both the original text string and the modified text string is used in a bash script to provide a message for git commits. Where the script runs it will have access to standard linux tools.
#/usr/bin/env bash
CommitMessage will be parsed as script argument
CommitMessage="This is my git commit message with a multiline description."
CommitDescription will be parsed as script argument
CommitDescription="My long commit message description with text that needs to be broken down to shorter lines of text. The text should be only be breaked on word boundaries and keep the max line length to X characters"
Convert CommitDescription so it has \n line breaks at line boundaries.
LinebreakedCommitDescription="My long commit message description with text that needs to be broken down to shorter lines of text.\nThe text should be only be broken on word boundaries and keep the max line \nlength to X characters"
When used with literal string in the commit message it works.
git commit -m "$CommitMessage" -m $'My long commit message\ndescription with text\nthat needs to be broken down.'
Facing an issue when using a variable, then the text is not linebreaked correctly.
git commit -m "$CommitMessage" -m "${LinedbreakedCommitDescription}"
Example text without any line breaks.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras pulvinar metus sit amet placerat pulvinar. Nunc tincidunt, ante at ornare consequat, felis tellus vulputate ligula, a ullamcorper metus velit non elit. Integer lectus ligula, vulputate volutpat rutrum non, facilisis at lacus. Cras mollis augue neque, sit amet porta nunc maximus vel. Fusce rutrum orci nunc, vulputate sodales metus semper et. Aliquam tristique sapien a dapibus eleifend. Donec mollis scelerisque sodales. Maecenas lacinia molestie elit eu elementum. Phasellus a justo sollicitudin, commodo mauris vitae, ornare risus. Nunc suscipit, purus nec pulvinar malesuada, urna diam ornare sapien, vitae sagittis urna justo nec.
This is the expected result of the example text with line breaks inserted at word boundaries based on max line length of 70 characters. This one is hand made in a text editor.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras\npulvinar metus sit amet placerat pulvinar. Nunc tincidunt, ante at\nornare consequat, felis tellus vulputate ligula, a ullamcorper metus\nvelit non elit. Integer lectus ligula, vulputate volutpat rutrum non,\nfacilisis at lacus. Cras mollis augue neque, sit amet porta nunc\n
maximus vel. Fusce rutrum orci nunc, vulputate sodales metus semper\net. Aliquam tristique sapien a dapibus eleifend. Donec mollis\nscelerisque sodales. Maecenas lacinia molestie elit eu elementum.\nPhasellus a justo sollicitudin, commodo mauris vitae, ornare risus.\nNunc suscipit, purus nec pulvinar malesuada, urna diam ornare sapien,\nvitae sagittis urna justo nec.
The resulting output of a git log would look something like this.
This is my git commit message with a multiline description.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras
pulvinar metus sit amet placerat pulvinar. Nunc tincidunt, ante at
ornare consequat, felis tellus vulputate ligula, a ullamcorper metus
velit non elit. Integer lectus ligula, vulputate volutpat rutrum non,
facilisis at lacus. Cras mollis augue neque, sit amet porta nunc
maximus vel. Fusce rutrum orci nunc, vulputate sodales metus semper
et. Aliquam tristique sapien a dapibus eleifend. Donec mollis
scelerisque sodales. Maecenas lacinia molestie elit eu elementum.
Phasellus a justo sollicitudin, commodo mauris vitae, ornare risus.
Nunc suscipit, purus nec pulvinar malesuada, urna diam ornare sapien,
vitae sagittis urna justo nec.
fold
is for, specificallyfold -sw70
in your case. See the answers of the duplicate for details. – terdon Feb 04 '22 at 13:06CommitMessage=$(printf '%s\n' "$CommitMessage" | fold -sw70)
for instance. – Stéphane Chazelas Feb 04 '22 at 13:11git commit -am "$CommitMessage" -m "$(printf '%s' "$CommitDescription" | fold -sw70)"
Using the fold command directly within the commit message made it much simpler. I haven't found this particular solution being described elsewhere. So if you would like add it as an answer I'm find with it.
– RLindquist Feb 04 '22 at 13:59fold -sw70
" is the same answer whether the input to it comes from a file or a pipe or a here-string or a here-doc or any other way of providing the input. – Ed Morton Feb 04 '22 at 14:52