Bash “制造回声”;“自动填充”;一定宽度的线

Bash “制造回声”;“自动填充”;一定宽度的线,bash,echo,Bash,Echo,我有一个相当大的bash脚本,它读入许多变量,然后进行响应。我没有办法事先知道这些变量的长度,我也不想缩短它们或以任何方式格式化它们——我只想让它们以相同的形式被读取 为了提高脚本输出的可读性,我重复了一些行来帮助眼睛,如下所示: echo "--------------------------------" echo "Here come some variables |" echo " |" echo "a = $a a

我有一个相当大的bash脚本,它读入许多变量,然后进行响应。我没有办法事先知道这些变量的长度,我也不想缩短它们或以任何方式格式化它们——我只想让它们以相同的形式被读取

为了提高脚本输出的可读性,我重复了一些行来帮助眼睛,如下所示:

echo "--------------------------------"
echo "Here come some variables       |"
echo "                               |"
echo "a = $a and b = $b"
echo "                               |"
echo "End of the script              |"
echo "--------------------------------"
--------------------------------
Here come some variables       |
                               |
a = 1.23e-19 and b = hello     |
                               |
End of the script              |
--------------------------------
这将输出:

--------------------------------
Here come some variables       |
                               |
a = 1.23e-19 and b = hello
                               |
End of the script              |
--------------------------------
我想要实现的是让echo(或其他替代解决方案)识别在打印变量后需要插入多少空格,这样它就可以将一条垂直线
|
与其余变量对齐,这样输出结果如下所示:

echo "--------------------------------"
echo "Here come some variables       |"
echo "                               |"
echo "a = $a and b = $b"
echo "                               |"
echo "End of the script              |"
echo "--------------------------------"
--------------------------------
Here come some variables       |
                               |
a = 1.23e-19 and b = hello     |
                               |
End of the script              |
--------------------------------

它不需要是一个
echo
唯一的解决方案,但我希望简单。有什么想法吗?

我现在没有访问linux机器的权限,但是还有可以在命令行上使用的
printf
。因此,您可以使用
printf“a=%10s和b=%10s”\n“$a“$b”


您可以使用格式说明符来改善结果。

我现在没有访问linux机器的权限,但是还有可以在命令行上使用的
printf
。因此您可以使用
printf“a=%10s和b=%10s”\n“$a“$b”


您可以使用格式说明符来改进结果。

因此,使用
printf
详细说明Axel的答案可以做到这一点。如果线宽为线宽,则:

a="1.23e-19" 
b="hello"
linewidth=30
echo "--------------------------------"
echo "Here come some variables       |"
echo "                               |"
printf "%-${linewidth}s |\n" "a = $a and b = $b"
echo "                               |"
echo "End of the script              |"
echo "--------------------------------"
这正是我想要的。输出:

--------------------------------
Here come some variables       |
                               |
a = 1.23e-19 and b = hello     |
                               |
End of the script              |
--------------------------------

因此,使用
printf
详细说明Axel的答案可以做到这一点。如果线宽为线宽,则:

a="1.23e-19" 
b="hello"
linewidth=30
echo "--------------------------------"
echo "Here come some variables       |"
echo "                               |"
printf "%-${linewidth}s |\n" "a = $a and b = $b"
echo "                               |"
echo "End of the script              |"
echo "--------------------------------"
这正是我想要的。输出:

--------------------------------
Here come some variables       |
                               |
a = 1.23e-19 and b = hello     |
                               |
End of the script              |
--------------------------------

省略尾随“|”怎么样?如果我想省略|我就不会问这个问题,因为这是我目前使用的解决方案。省略尾随“|”怎么样?如果我想省略|我就不会问这个问题,因为这是我目前使用的解决方案。谢谢,但我不想更改变量的格式。@Miguel:我指的是宽度(在我的示例中为10个字符)和对齐方式(在左对齐的百分号后插入
-
,否则使用右对齐)。如果希望该变量左对齐,可以使用“%-5s”。谢谢,但我不想更改变量的格式。@Miguel:我指的是宽度(在我的示例中为10个字符)和对齐方式(在左对齐的百分号后插入
-
,否则使用右对齐)。如果希望该变量左对齐,可以使用“%-5s”。您不需要
temp
。只需使用
printf“%-${linewidth}s| \n”a=$a和b=$b”
您不需要
temp
。只需使用
printf“%-${linewidth}s|\n”a=$a和b=$b”