如何在同一行上打印bash数组 [英] How to print a bash array on the same line

查看:298
本文介绍了如何在同一行上打印bash数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将文件类型数据读入bash数组中,并且需要将其内容打印在同一行中并带有空格.

I am reading in filetype data into a bash array and need to print its contents out on the same line with spaces.

#!/bin/bash

filename=$1
declare -a myArray

readarray myArray < $1

echo "${myArray[@]}" 

我尝试这样做,即使使用echo -n标志,它仍然会打印在换行符上,我还缺少什么,printf会更好地工作吗?

I try this and even with the echo -n flag it still prints on newlines, what am I missing, would printf work better?

推荐答案

readarray在每个数组元素中保留尾随换行符.要删除它们,请使用-t选项.

readarray retains the trailing newline in each array element. To strip them, use the -t option.

readarray -t myArray < "$1"

这篇关于如何在同一行上打印bash数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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