Bashで複数行にわたって文字列を連結する方法は?

Bashで複数行にわたって文字列を連結する方法は?

script.shから:

#!/bin/bash
# Above is a while statement, so the printf has a indent(actually are 3 spaces)
   printf "I am a too long sentence in line1, I need to switch to a new line. \n\
   I'd like to be at the head of this line2, but actually there are 3 redundant spaces \n"

コードで述べたように、次のように表示されます。

I am a too long sentence in line1, I need to switch to a new line.
      I'd like to be at the head of this line2, but actually there are 3 redundant spaces

printfこの問題を解決するには、すべての行にaを使用する必要があります。良い:

   printf "I am a too long sentence in line1, I need to switch to a new line. \n"
   printf "I'd like to be at the head of this line2, but actually there are 3 redundant spaces \n"

あまりにも愚かなようですが、なぜ以下のように文字列を連結するのですか?

   printf {"a\n"
   "b"}

   # result should be like below
a
b

ベストアンサー1

printf一般的な方法を使用できます。最初の文字列は型を定義し、次の文字列はパラメータです。

例:

#!/bin/bash
   printf '%s\n' "I am a too long sentence in line1, I need to switch to a new line."\
      "I'd like to be at the head of this line2, but actually there are 3 redundant spaces."\
            "I don't care how much indentation is used"\
 "on the next line."

出力:

I am a too long sentence in line1, I need to switch to a new line.
I'd like to be at the head of this line2, but actually there are 3 redundant spaces.
I don't care how much indentation is used
on the next line.

おすすめ記事