72

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.

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.

function affiche_message(){
    #Personnalisation de la fenêtre
    $DIALOG --title "$1" \
            --msgbox "$2" 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.

1 Answer 1

130

Use * as the subscript to expand the array as a single word with the values of each element separated by the first character of the IFS special variable:

"${tabSuppr[*]}"

See man bash for explanation.

Sign up to request clarification or add additional context in comments.

6 Comments

Any way to get each item on its own line?
@zundi: The following seems to work: ( IFS=$'%'; ajout="${tabAjout[*]}"; suppr="${tabSuppr[*]}" ; affiche_message "Title" "Delete : ${suppr//%/\\n}\n\nAdd : ${ajout//%/\\n}" ; ).
You got my upvote for the answer but "See man bash for explanation." not very usefull at all... can anyone explain this?
@eladsilver: When you use * as an array index in double quotes, bash expands the array as a single word, separating the elements by the first character of the special variable $IFS.
@zundi "Any way to get each item on its own line?" echo "${tabSuppr[*]}" | sed 's| |\n|g'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.