Note that it's not generally a good idea to read lines of input from a file or command with a for
loop. You should generally use a while+read
Although in this particular case the expansion splitting the file into individual words results in the desired behavior. If the file were very large, however, it could possibly overwhelm the capacity of the terminal, as the whole list gets expanded before the for loop is run.
As mentioned, grep
can also be used to test one file against another (on a per-line basis). This command prints every line in file2 that does not exist in file1:
grep -v -f file1.txt file2.txt
Just run the command again with the files reversed to get all the unique lines in file1.