Array operators

Hi

Lets say I have two arrays:
VAR_1 = "File_A" "File_B" "File_C" "File_D"
VAR_2 = "File_A" "File_D"

Is there a simple command to get the difference of these list, i.e.
VAR_1_2 = "File_B" "File_C"
or do I have to write a script and loop through all elements and compare them one by one?

Thanks

For most shell languages neither of those statements make an array. You cannot have spaces on any side of the = character:

bash array:

var_1=( "array element 1"  "array element 2")

ksh array:

set -A var_1 "array element 1"  "array element 2"

The answer to your question is No, there is no single command, you have to program a couple of pipes:

one way assuming you have the arrays already and they are array and array2:

echo ${array1[*]} | tr -s ' ' '\n' > file1
echo ${array2[*]} | tr -s ' ' '\n' > file2
diff file1 file2

I am assuming you want real differences. If you simply want to know if they are equal:

if [  "${array1[*]}" = "${array2[*]}"  ] ; then
   echo "same"
else 
  echo "different"
fi

Not sure what you wanted, really.

1 Like