bash 如何在bash数组中存储多行输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6848791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:28:06  来源:igfitidea点击:

How to store multiple row output in a bash array?

bashshellsqlplus

提问by batty

I have a select statement

我有一个选择语句

sqlplus [credentials] select variable from table;

It returns 6 rows and I need to store them as an array in bash array variable.

它返回 6 行,我需要将它们作为数组存储在 bash 数组变量中。

回答by jman

array=(`sqlplus [credentials] select variable from table;`)
echo ${array[*]}

回答by mivk

If your variables contain spaces and you want the array to have an element for each line of output (as opposed to each word of output), you also need to set your IFS. And you may want to use quotes when using the array:

如果您的变量包含空格并且您希望数组的每一行输出都有一个元素(而不是输出的每个单词),您还需要设置 IFS。并且您可能希望在使用数组时使用引号:

SaveIFS="$IFS"

IFS=$'\n'
array=( $(sqlplus [credentials] select variable from table;) )
echo "${array[*]}"

IFS="$SaveIFS"