I'm trying to compare the contents of two files in a bash script.
local_file=$(cat my_local_file.txt)
remote_file=$(curl -s "http://example.com/remote-file.txt")
if [ local_file == remote_file ]; then
echo "Files are the same"
else
echo "Files are different. Here is the diff:"
diff <(echo "$local_file") <(echo "$remote_file")
fi
When I run the script, I see that I have a syntax error:
./bin/check_files.sh: line 8: syntax error near unexpected token `('
./bin/check_files.sh: line 8: ` diff <(echo "$local_file") <(echo "$remote_file")'
What am I doing wrong? How can I display a diff of these two strings from a bash script?
$(...)doesn't preserve trailing newlines or NUL characters, so the contents oflocal_fileandremote_filecould potentially not reflect what's on-disk.diffdeal with NUL characters? I suspect not.diffprogram that fails to do so broken. GNU diff does indeed work with NUL characters, trydiff <(printf 'foo\0bar') <(printf 'foo\0\0bar').diff(see "Diff Binary Output Format") explicitly supports binary files.