如何使用 bash 大括号扩展制作乘法表?到目前为止我有这个: echo $[{1..10}*{1..10}]
我想更深入地学习 bash,所以我决定制作一个乘法表。我具有该语句的功能:
echo $[{1..10}*{1..10}]
但这给了我以下输出:
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
有什么方法可以仅使用 1 个语句来格式化此输出,如下所示(我可以弄清楚如何使用循环来执行此操作,但这并不有趣:p)
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
是否可以在一条语句中完成,或者我必须循环?
I am trying to learn bash at a deeper level, and I decided to make a multiplication table. I have the functionality with the statement :
echo $[{1..10}*{1..10}]
but that gives me the following output:
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
Is there any way to format this output like the following using only 1 statement (i can figure out how to do this with loops, but that's no fun :p )
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Is it even possible to do in one statement, or would I have to loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用此行可以在不使用循环的情况下获得良好的输出:
echo $[{1..10}*{1..10}] | xargs -n10 | xargs -n10 | column -t
输出:
更新
作为合乎逻辑的下一步,我问这里如果这个乘法表可以有一个可变范围。通过此帮助,我的答案适用于变量(
$boundary
) 范围并保持相当可读:boundary=4; eval echo $\[{1..$boundary}*{1..$boundary}\] | xargs -n$边界| column -t
输出:
另请注意,
$[..]
算术符号已弃用,应使用$((...))
代替:边界=4; eval eval echo "$\(\({1..$boundary}*{1..$boundary}\)\)" | xargs -n$边界|列-t
Use this line for a nice output without using loops:
echo $[{1..10}*{1..10}] | xargs -n10 | column -t
Output:
Update
As a logical next step, I asked here if this multiplication table can have a variable range. With this help, my answer works with a variable (
$boundary
) range and stays quite readable:boundary=4; eval echo $\[{1..$boundary}*{1..$boundary}\] | xargs -n$boundary | column -t
Output:
Also note that the
$[..]
arithmetic notation is deprecated and$((...))
should be used instead:boundary=4; eval eval echo "$\(\({1..$boundary}*{1..$boundary}\)\)" | xargs -n$boundary | column -t
printf
内置函数会根据需要多次重复其格式以打印所有参数,因此:如果您想避免重复
%d
位,那就比较棘手了。在生产代码中,使用循环。
The
printf
built-in repeats its format as many times as necessary to print all arguments, so:If you want to avoid repeating the
%d
bit, it's trickier.In production code, use a loop.