Search code examples
unixdiffsdiff

Finding the differences of two variables containing strings unix


How i use diff for variables instead of files.

All tutorials have examples with files but not with variables.

I want it to print just the differences.

for example:

TEXTA=abcdefghijklmnopqrstuvxyz; TEXTB=abcdefghijklmnopqrstuvxyr

Solution

  • diff is a utility to compare two files. If you really want to compare two variables, and you are using bash for your shell, you can "fake it" this way:

    diff <(echo ${TEXTA}) <(echo ${TEXTB})
    

    Otherwise, you can just write your variables to two temporary files and compare them.

    However, note that in your example, since each variable is a single line, it'll just tell you that they're different, unless you use a version of diff that will show you the specific positions in the line where they differ.