1

I have a bash string that looks like this:

string="This is the first line.
This is the second line"

I would like to convert this into a git commit message, so I need to insert a blank line after the first line to make it the commit title, and the second line the commit body, so that the expansion looks like this:

This is the first line.

This is the second line

What is the simplest way to achieve this in bash?

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
Tri Nguyen
  • 9,950
  • 8
  • 40
  • 72
  • If you're getting the above string as an output of the variable, then it actually has newlines present – Inian Feb 07 '19 at 17:26
  • Like [this](https://stackoverflow.com/questions/5064563/add-line-break-to-git-commit-m-from-the-command-line)? (There are few answers here depending on your need) – JNevill Feb 07 '19 at 17:29
  • 1
    @JNevill, I'm not sure it's a duplicate; I guess OP wants a _blank_ line between first and second line (not too sure, though). Tri Nguyen, can you confirm you want a blank line between the two? – gniourf_gniourf Feb 07 '19 at 17:34
  • @gniourf_gniourf I can't see how that would make a difference. Each of those answers could be slightly massaged to include a second blank line between the two strings. – JNevill Feb 07 '19 at 17:35
  • 2
    @JNevill: (I don't know, I'm just wild guessing here): maybe OP has this string already, and wants to insert a blank line between the first line and the rest of the string? (again, I really don't know, just guessing). – gniourf_gniourf Feb 07 '19 at 17:37
  • 3
    @JNevill, I agree this is not a duplicate of the git commit question. OP says he has a *bash* string already and wants to insert a new line. To boot, there is no git tag. I believe he was just giving context. – Bill Doughty Feb 07 '19 at 17:47
  • So this question is really more like a `tr '\n', '\n\n'` type of thing and we are assuming this "String in bash" is a variable holding two lines? I would suggest voting to reopen (I can vote, but can't reopen on my own), but it may just get closed again as unclear. – JNevill Feb 07 '19 at 18:28
  • @JNevill: no problems, we can reopen it if needed; I'd just like to have OP's confirmation `:)`. (and then we might slightly edit the question to make it clearer). – gniourf_gniourf Feb 07 '19 at 18:38
  • THanks. @BillDoughty and gniourf are correct. This is not a git commit question. I already have the string in bash. I'm just looking to insert the blank line. The git commit is for context. – Tri Nguyen Feb 07 '19 at 18:57

1 Answers1

4

This should do (it replaces the first newline character found by two consecutive newline characters):

string="This is the first line.
This is the second line"
new_string=${string/$'\n'/$'\n\n'}
echo "$new_string"

The ${var/pattern/replacement} is a Parameter Expansion; it expands to the expansion of var where the first occurrence of pattern is replaced by replacement.

The $'...' is known as ANSI-C quoting and will allow escape sequences.

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104