我可以使用 echo 和 bash 对齐字符串中的变量吗?

发布于 2024-08-23 05:49:02 字数 445 浏览 3 评论 0原文

我想在 shell 脚本中使用 echo 进行格式化。

这是一个给我带来问题的小代码片段:

echo -en "\rFileName    :   $filename   :    $index of $lines Completed"

$filename 是一个长度不同的字符串,这导致终端中的格式出现问题。我怎样才能克服这个问题?

这就是我的意思:

FileName :       a800_102 :    6 of 6 Completed
FileName :       ersf_1024    :    56 of 56 Completed

当我在终端上显示它时,我希望有一个表格格式。

I want to do formatting using echo in shell scripting.

Here is a small code snippet which is giving me the problem:

echo -en "\rFileName    :   $filename   :    $index of $lines Completed"

The $filename is a string with varying length, and this is causing problem with formatting in the terminal. How can I overcome this?

Here's what I mean:

FileName :       a800_102 :    6 of 6 Completed
FileName :       ersf_1024    :    56 of 56 Completed

I would like to have a table format when I display it on the terminal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江城子 2024-08-30 05:49:02

使用 printf

printf "\rFileName : %20s : %8d of %8d Completed" $filename $index $lines

Use printf:

printf "\rFileName : %20s : %8d of %8d Completed" $filename $index $lines
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文