Please define your needs a little more. What exactly do you mean by differences? Is it the first position in each array that has a non-matching element? The first item in one array that's not contained in the other? Or something else?
Second, what is your actual problem? Is there anything wrong with the current code? Or are you just asking if there's a better solution?
To start with a few observations though:
1) When you use the "
[*]" index, you output the whole array as a single string. Then since you haven't quoted any of them, the shell does re-splits them on whitespace into separate words again. That's why there's also "
[@]", which outputs the whole array as a list of individual items, that when quoted act as if they are individually quoted.
So always use "
[@]", with quotes, unless you have a special need to do otherwise.
2) The exclamation point in pattern "
${!array[*]}" (or
${!array[@]}, with the same behavior as #1 above) spits out a list of the array's index numbers. This means the loop variable will contain only a number, not the value. You probably do not want this.
3) This line:
Code:
new_name=( ${!name[*]} - ${!all_name[*]}
#1 and #2 apply, of course. It's also missing the closing bracket. But most importantly, you can't just "subtract" one array from another. So all this does is set the new array to all the index numbers of the first array (after word-splitting), then a hyphen, and then all the indexes of the second array. And it does it over and over for each of the double-loop iterations.
What you really want to do is run a
test of some kind to compare the strings contained in both array elements, and only print out the ones you want.
So perhaps something like this would be more appropriate.
Code:
all_name=( M1 M2 M3 M4 M5 M6 M7 M8 M9 M10 )
name=( M1 M2 M3 M8 )
for x in "${all_name[@]}"; do
[[ "${name[*]}" != *$x* ]] && new_name+=( "$x" )
done
echo "${new_name[@]}"
Note though that this is just a quick example. For one thing it depends on testing each individual element in
all_name against the full string of
name. If a single
name entry ever contained as a substring the whole of an entry in
all_name, it would produce a false negative.