如何处理长字符串并仍然在 bash 中获得格式良好的代码?

How to deal with long strings and still get a well formatted code in bash?

这个和我的 有点关系。现在我正在处理一个长字符串:

sed -b -i.old $inifile \
    -e "s/a_very_long_pattern_i_should_problably_improve/terribly_long_replace_string" 

有没有办法让它格式正确(即缩进且不超过 80 个字符)?使用反斜杠会增加额外的空格:

$ echo 'first_part'\
>      'second_part'
first_part second_part
$ echo 'first_part'\
>      'second_part'
first_part second_part

Bash 连接相邻的字符串文字。

例如,如果您有 echo "first_part" "second_part",您将得到

first_part second_part

但是,如果你有 echo "first_part""second_part" 你会得到 first_partsecond_part

所以为了您的

利用它
   -e "s/a_very_long_pattern_i_should_problably_improve/terribly_long_replace_string"

你可以尝试做类似的事情

sed -b -i.old $inifile \  
    -e "s/a_very_long_pattern_"
                   "i_should_problably_improve/"
                   "terribly_long_replace_string" 

您可以使用 printf 将一堆参数粘贴在一起:

sed -b -i.old $inifile \
    -e "$(printf %s
       's/long long pattern'
        '/long long replacement/;'
       's/another/command/'
       )"