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

查看:63
本文介绍了如何在同一行打印 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天全站免登陆