What is the difference between `"${arr[@]}"` and `"${arr[*]}"`?
IntermediateConceptArrays, conditionals and loops
Answer
"${arr[@]}" expands to one word per element, preserving spaces. "${arr[*]}" joins all elements into a single word separated by the first character of IFS.
files=("a b.txt" c.txt)
ls -l "${files[@]}" # two arguments
(IFS=,; echo "${files[*]}") # a b.txt,c.txt (join in a subshell)
Use [@] to pass elements to commands and [*] to build a display string.
More on Arrays, conditionals and loops
How do you declare, populate and iterate an indexed array?BeginnerHow do associative arrays work and what are they good for in DevOps scripts?IntermediateHow do you read a command's output into an array correctly?IntermediateWhat is the difference between `[ ]`, `[[ ]]` and `(( ))`?BeginnerWhy does `[ $var = "prod" ]` fail when `var` is empty or contains spaces?IntermediateList the most-used file test operators.Beginner