String difference in Bash

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.

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

String difference in Bash, string1 =~ regex - The regex operator returns true if the left operand matches the extended regular expression on the right. How to Compare Strings in Bash Difference between Integers and Strings. Now in bash we have strings and integers. So any text provided under single Comparison operators for strings. First let us understand the different comparison operator available for sting Check if Strings are Equal. Bash

Reminds me of this question: How can you diff two pipelines in Bash?

If you are in a bash session, you could do a:

diff <cmd1 <cmd2
diff <(foo | bar) <(baz | quux)

with < creating anonymous named pipes -- managed by bash -- so they are created and destroyed automatically, unlike temporary files.

So if you manage to isolate your two different string as part of a command (grep, awk, sed, ...), you can do - for instance - something like:

diff < grep string1 myFile < grep string2 myFile

(if you suppose you have in your file lines like string1=very_complicated_value and a string2=another_long_and_complicated_value': without knowing the internal format of your file, I can not recommend a precise command)

Elegant way of diff'ing two variables?, Using diff or com or whatever you want: diff <(echo "$string1" ) <(echo "$string2"). Greg's Bash FAQ: Process Substitution. or with a named pipe You can check the equality and inequality of two strings in bash by using if statement. “ == ” is used to check equality and “ != ” is used to check inequality of the strings. You can partially compare the values of two strings also in bash. How you can compare the string values in bash is shown using various examples in this tutorial.

I prefer cmp and Process Substitution feature of bash:

$ cmp -bl <(echo -n abcda) <(echo -n aqcde)
  2 142 b    161 q
  5 141 a    145 e

Saying on position 2, a b occurs for the first, but a q for the second. At position 5, another difference is happening. Just replace those strings by variables, and you are done.

How to compare strings in Bash – Linux Hint, But I have the district feeling that I am missing a clever Bash syntax to do that (as in Trying to output a diff of two strings using this syntax and I get "syntax error� When comparing strings in Bash you can use the following operators: string1 = string2 and string1 == string2 - The equality operator returns true if the operands are equal. Use the = Use the = operator with the test [ command. Use the == operator with the [ [ command for pattern matching. string1

Say you have three strings

a="this is a line"
b="this is"
c="a line"

To remove prefix b from a

echo ${a#"$b"}  # a line

To remove suffix c from a

echo ${a%"$c"}  # this is

bash - Find difference between two variables, For different programming purposes, we need to compare the value of two strings . Built-in Duration: 6:23 Posted: May 4, 2018 To split a string in bash using IFS, follow the below steps: Set IFS to the delimiter you would want. IFS='<delimiter>' IFS is an internal variable that determines how Bash recognizes word boundaries. The default value of IFS is white space.

Another example:

before="184613 102050 83756 63054"
after="184613 102050 84192 83756 63054"

comm -23 <(tr ' ' $'\n' <<< $after | sort) <(tr ' ' $'\n' <<< $before | sort)

Outputs

84192

Original answer here

Quickly Diff Two Strings in Bash Shell, stringwhat are you not able to find with man <command> you can try to find with man bash or with help command Ad nauseam: diff style� Note that there is no upper limit (maximum) on the size (length) of a Bash array and the values in an Indexed Array and an Associative Array can be any strings or numbers, with the null string being a valid value. 👉 Remember that the null string is a zero-length string, which is an empty string.

How to Compare Strings in Bash, Hey everyone! I am working on a Raspberry Pi 3 project (my first, I'm embarrassed to say :-p) and downloaded NOOBS OS installer to install� Bash Strings Equal – In this tutorial, we shall learn how to check if two strings are equal in bash scripting. Bash Strings Equal. To check if two strings are equal in bash scripting, use bash if statement and double equal to == operator. To check if two strings are not equal in bash scripting, use bash if statement and not equal to!= operator.

Other Comparison Operators, integer and string use a different set of comparison operators. Let us take some examples to� If the value of IFS is the empty string, the separator is the empty string.) If there are no positional parameters then "$*" is the empty string, if there are two positional parameters and IFS has its default value then "$*" is equivalent to "$1 $2", etc. $@ and $* outside quotes are equivalent.

diff, diffh, bdiff -- compare two text files and show differences, Note that integer and string comparison use a different set of operators. There is some blurring between the arithmetic and string comparisons, #+ since Bash� And, item 3 within the array points to "Apr" (remember: the first index in an array in Bash is [0]). That means that echo ${month[3]} , after the expansion, translates to echo "Apr" . Interpreting a variable as its value is one way of expanding it, but there are a few more you can leverage.

Comments
  • what is it you actually want to do?
  • You can use substring manipulations and builtin test operations with IFS changes to compare, but you would need to know if you want to compare character by character, word by word, line by line, ignore white space ...
  • See stackoverflow.com/questions/34376884/…
  • +1 for correct answer. +1 for great explanation of symbols. Additionally, Greg's Bash FAQ has moved to: mywiki.wooledge.org The links for the above pages are now at mywiki.wooledge.org/ProcessSubstitution and mywiki.wooledge.org/BashFAQ/085
  • thx! and also, this will show the dynamic file descriptors FUNC(){ echo "$@"; "$@"; }; FUNC diff <(echo a) <(echo b);
  • I was looking for that for compairing two shasums. Not sure if there is a more elegant way to do that, but it works.
  • This seems to work if there are multiple lines in $string1 and $string2, and diff outputs the lines that were added or subtracted. What if the string is a single line, and line and there is some difference between the two strings?
  • @alpha_989 , here's your answer: $ diff <(echo "Here are the letters in String One.") <(echo "Here are the characters in String Two.") \n 1c1 \n < Here are the letters in String One. \n --- \n > Here are the characters in String Two. \n Using the pipe is similar, except it shows a process number, starts with the 1c1 after the next $, and waits until you press <kbd>Enter<kbd> (or you can do other commands...)
  • This works only when strings are of the same length!
  • I guess this is the bash way of doing it. It worked nicely. That syntax is a bit hard to grasp though.
  • @MikaelRoos Agreed. Easier to read (for me anyway) would be to use sed: echo "$a" | sed "s!^$b!!g" (I swapped out the standard sed separator / for ! in case the variables being dealt with are paths. Also, you could use a here string instead of echo: sed ... <<< $a.)