I have two variables input:VAR1=abc red blue cat empty dummy rummyVAR2=rummy zero empty rat cat reverseoutput:I want output as follows: (remove the common ones from $VAR2 in $VAR1)abc red blue dummyI tried as follows:for i in $VAR1do        for j in $VAR2        do                if [ $i != $j ]; then                        echo $i;                fi        donedoneHere, if [ $i == $j ]; then echo $i, here I get output perfectly as cat empty rummyBut I need to get output other than these.
I want to compare two list and print the difference output from list one
shell script;test
Try:    a=()    for i in $VAR1; do        found=        for j in $VAR2; do            if [ $i == $j ]; then                found=1                break            fi        done        if [ ! $found ]; then            a+=($i)        fi    done    echo ${a[*]}In words: For every i in VAR1, compare it against every j in VAR2. If no match is found, add i to the output.This version is assuming the characters in VAR1 and VAR2 won't be confusing the shell. Also, it is inefficient running in quadratic time, but maybe that's not a concern.Faster, using associative arrays:declare -A a2for k in $VAR2; do    a2[$k]=1donefor k in $VAR1; do    if [ ! ${a2[$k]} ]; then        echo $k    fidone