我有一个bash的字符串,如下所示:
string="This is the first line.
This is the second line"
我想这个转换成一个Git提交信息,所以我需要插入一个空行的第一行后,使其提交的标题,第二行提交身体,使扩张看起来是这样的:
This is the first line.
This is the second line
什么是在bash实现这一目标的最简单的方法?
这应该做(它取代由两个连续的换行符找到的第一个换行符):
string="This is the first line.
This is the second line"
new_string=${string/$'\n'/$'\n\n'}
echo "$new_string"
所述${var/pattern/replacement}
是Parameter Expansion;它扩展到var
的第一次出现是由pattern
取代replacement
的扩张。
所述$'...'
称为ANSI-C quoting并且将允许转义序列。