如何在BASH列的输出数组内容 [英] How to output an array's content in columns in BASH

查看:206
本文介绍了如何在BASH列的输出数组内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个数组显示字符串的一个长长的清单。

I wanted to display a long list of strings from an array.

现在,我的脚本通过运行循环呼应每个值到标准输出:

Right now, my script run through a for loop echoing each value to the standard output:

for value in ${values[@]}
do
  echo $value
done

是啊,这是pretty难看!而一列列表pretty长的太...

Yeah, that's pretty ugly! And the one column listing is pretty long too...

我想知道如果我能找到一个命令或者内建帮助我在列中显示所有这些值,如 LS 命令默认列出一个目录(什么时候 LS -C )。

I was wondering if i can find a command or builtin helping me to display all those values in columns, like the ls command does by default when listing a directory (ls -C).

[更新]

失去我的大脑与显示不正确格式化列,这里有更多的信息:

Losing my brain with column not displaying properly formatted columns, here's more info:

值: $值=(01 ---- 02 7 7 ---- 03 ----- 04 8 7 ---- 05 ----- 8 06- ---- 8 07 ----- 08 8 8 ----- 09 --- 6 10 ---- 7 11 ---- 7月12日---- 7 ---- 13 14- 7 ---- 8月15日----- 8月16日---- 7月17日---- 7月18日--- 6月19日----- 8月20日----- 8月21日8 ----- )(注意前两个数字作为索引,最后一个指示可读性字符串的长度)

命令:回声$ {值[@] /%/ $的'\\ n'}|列

结果:

The result:

事情错了......

Something is going wrong...

推荐答案

您可以管你的输出到

You could pipe your output to column.

似乎单列输入比一个制表位(8个字符)窄一些数据奋斗。

column seems to struggle with some data in a single-column input being narrower than a tabstop (8 characters).

使用 的printf 内的 -loop填充值,为8个字符,似乎这样的伎俩:

Using printf within a for-loop to pad values to 8 characters seems to do the trick:

for value in "${values[@]}"; do 
    printf "%-8s\n" "${value}"
done | column

这篇关于如何在BASH列的输出数组内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆