String difference in Bash


Question

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.

1
95
10/6/2018 5:26:05 AM

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

164
11/22/2015 11:15:29 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon