bash 如何在bash中回显数组中的所有值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41150814/
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-08 22:28:36  来源:igfitidea点击:

How to echo all values from array in bash

arraysbashdialog

提问by Mattasse

I am making a bash script using dialog. My script make the difference between files in two tar.gz. Each add files are put in an array and each delete files are put in an other array.

我正在使用对话框制作 bash 脚本。我的脚本使两个 tar.gz 中的文件有所不同。每个添加文件都放在一个数组中,每个删除文件都放在另一个数组中。

All files are add in my two array and when I want echo them it's works

所有文件都添加到我的两个数组中,当我想回显它们时它的工作原理

echo ${tabAjout[@]}
echo ${tabSuppr[@]} 

The output is :

输出是:

bonjour.txt.gpg test2.txt.gpg test.txt.gpg
hello.txt.gpg

Now I want add this in msgbox.

现在我想在 msgbox 中添加它。

function affiche_message(){
    #Personnalisation de la fenêtre
    $DIALOG --title "" \
            --msgbox "" 20 45
}

Call function :

调用函数:

affiche_message "Title" "Delete : ${tabSuppr[@]} \n\n Add : ${tabAjout[@]}"

When I run my script the msgbox contains only the first values of the array. If I change ${tabAjout[@]} by ${#tabAjout[@]} the dialog windows echo that array contains 3 values.

当我运行我的脚本时,msgbox 只包含数组的第一个值。如果我将 ${tabAjout[@]} 更改为 ${#tabAjout[@]},对话框窗口会回显该数组包含 3 个值。

回答by choroba

Use *as the subscript to expand the array as a single word:

使用*的标展开数组为一个字:

"${tabSuppr[*]}"

See man bashfor explanation.

请参阅man bash说明。