フォーマットされた形式で出力を取得する

フォーマットされた形式で出力を取得する

forループ内では、次を使用します。

echo " Job_time is $i : Stopping the job "

出力例:

Job_time is 6 : Stopping the job
Job_time is 6.50 : Stopping the job

希望の出力:

Job_time is 6    : Stopping the job
Job_time is 6.50 : Stopping the job

ベストアンサー1

echoprintf代わりに、特定のフィールドの幅を指定できます。与えられた例では

echo " Job_time is $i : Stopping the job "

そして

printf ' Job_time is %-4s : Stopping the job \n' "$i"

$i明らかに長さが異なる文字列だからです。常に有効な浮動小数点数の場合は、数値書式を使用して次のprintfゼロを表示できます。

printf ' Job_time is %4.2f : Stopping the job \n' "$i"

例えば、

Job_time is 6.00 : Stopping the job
Job_time is 6.50 : Stopping the job

追加資料:

おすすめ記事