Skip to main content

Yes, you can use diff on two strings, if you make files from them, because diff will only ever compare files.

A shortcut way to do that is using process substitutions in a shell that supports these:

diff <( printf '%s\n' "$string1" ) <( printf '%s\n' "$string2" )
diff <( printf '%s\n' "$string1" ) <( printf '%s\n' "$string2" )

Example:

$ diff <( printf '%s\n' "hey" ) <( printf '%s\n' "hi" )
1c1
< hey
---
> hi

In other shells,

printf '%s\n' "$string1" >tmpfile
printf '%s\n' "$string2" | diff tmpfile -
rm -f tmpfile
printf '%s\n' "$string1" >tmpfile
printf '%s\n' "$string2" | diff tmpfile -
rm -f tmpfile

In this second example, one file contains the first string, while the second string is given to diff on standard input. diff is invoked with the file containing the first string as its first argument. As its second argument, - signals that it should read standard input (on which the second string will arrive via printf).

Yes, you can use diff on two strings, if you make files from them, because diff will only ever compare files.

A shortcut way to do that is using process substitutions in a shell that supports these:

diff <( printf '%s\n' "$string1" ) <( printf '%s\n' "$string2" )

Example:

$ diff <( printf '%s\n' "hey" ) <( printf '%s\n' "hi" )
1c1
< hey
---
> hi

In other shells,

printf '%s\n' "$string1" >tmpfile
printf '%s\n' "$string2" | diff tmpfile -
rm -f tmpfile

In this second example, one file contains the first string, while the second string is given to diff on standard input. diff is invoked with the file containing the first string as its first argument. As its second argument, - signals that it should read standard input (on which the second string will arrive via printf).

Yes, you can use diff on two strings, if you make files from them, because diff will only ever compare files.

A shortcut way to do that is using process substitutions in a shell that supports these:

diff <( printf '%s\n' "$string1" ) <( printf '%s\n' "$string2" )

Example:

$ diff <( printf '%s\n' "hey" ) <( printf '%s\n' "hi" )
1c1
< hey
---
> hi

In other shells,

printf '%s\n' "$string1" >tmpfile
printf '%s\n' "$string2" | diff tmpfile -
rm -f tmpfile

In this second example, one file contains the first string, while the second string is given to diff on standard input. diff is invoked with the file containing the first string as its first argument. As its second argument, - signals that it should read standard input (on which the second string will arrive via printf).

added 116 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Yes, you can use diff on two strings, if you make files from them, because diff will only ever compare files.

A shortcut way to do that is using process substitutions in a shell that supports these:

diff <( printf '%s\n' "$string1" ) <( printf '%s\n' "$string2" )

Example:

$ diff <( printf '%s\n' "hey" ) <( printf '%s\n' "hi" )
1c1
< hey
---
> hi

In other shells,

printf '%s\n' "$string1" >tmpfile
printf '%s\n' "$string2" | diff tmpfile -
rm -f tmpfile

In this second example, one file contains the first string, while the second string is given to diff on standard input. diff is invoked with the file containing the first string as its first argument. As its second argument, - signals that it should read standard input (on which the second string will arrive via printf).

Yes, you can use diff on two strings, if you make files from them, because diff will only ever compare files.

A shortcut way to do that is using process substitutions in a shell that supports these:

diff <( printf '%s\n' "$string1" ) <( printf '%s\n' "$string2" )

In other shells,

printf '%s\n' "$string1" >tmpfile
printf '%s\n' "$string2" | diff tmpfile -
rm -f tmpfile

In this second example, one file contains the first string, while the second string is given to diff on standard input. diff is invoked with the file containing the first string as its first argument. As its second argument, - signals that it should read standard input (on which the second string will arrive via printf).

Yes, you can use diff on two strings, if you make files from them, because diff will only ever compare files.

A shortcut way to do that is using process substitutions in a shell that supports these:

diff <( printf '%s\n' "$string1" ) <( printf '%s\n' "$string2" )

Example:

$ diff <( printf '%s\n' "hey" ) <( printf '%s\n' "hi" )
1c1
< hey
---
> hi

In other shells,

printf '%s\n' "$string1" >tmpfile
printf '%s\n' "$string2" | diff tmpfile -
rm -f tmpfile

In this second example, one file contains the first string, while the second string is given to diff on standard input. diff is invoked with the file containing the first string as its first argument. As its second argument, - signals that it should read standard input (on which the second string will arrive via printf).

Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Yes, you can use diff on two strings, if you make files from them, because diff will only ever compare files.

A shortcut way to do that is using process substitutions in a shell that supports these:

diff <( printf '%s\n' "$string1" ) <( printf '%s\n' "$string2" )

In other shells,

printf '%s\n' "$string1" >tmpfile
printf '%s\n' "$string2" | diff tmpfile -
rm -f tmpfile

In this second example, one file contains the first string, while the second string is given to diff on standard input. diff is invoked with the file containing the first string as its first argument. As its second argument, - signals that it should read standard input (on which the second string will arrive via printf).