String difference in Bash

bash, string

Solution

Using `diff` or `com` or whatever you want:

diff  <(echo "$string1" ) <(echo "$string2")

Greg's Bash FAQ: Process Substitution

or with a named pipe

mkfifo ./p
diff - p <<< "$string1" & echo "$string2" > p

Greg's Bash FAQ: Working with Named Pipes

Named pipe is also known as a FIFO.

The `-` on its own is for standard input.

`<<<` is a "here string".

`&` is like `;` but puts it in the background

Problem

I'm trying to find a way to determine the difference between two strings in my script. I could easily do this with diff or comm, but I'm not dealing with files and I'd prefer not to output them to files, do the compare and read it back. I see that comm, diff, cmp all allow to pass either two files OR a file and standard input - I guess that's good if I don't want to output two files...but it's still kinda sucks. Been digging around thinking I can use grep or regular expressions - but I guess not.

Original source

Related problems