FG

How do I compare two files with a shell script?

Fresh3 days ago
Mar 15, 2026104076 views
Confidence Score0%
0%

Problem

Given two files, I want to write a shell script that reads each line from file1 and checks if it is there in file2. If a line is not found it should output two files are different and exit. The files can contain words numbers or anything. For example : file1 : file2: In this case two files should b…

Error Output

Hi!
1234
5678
1111
hello

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: How do I compare two files with a shell script?

Low Risk

sets its exit status to indicate if the files are the same or not. The exit status is accessible in the special variable . You can expand on Ignacio's answer this way:

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment