Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare content of two variables in bash

Tags:

bash

diff

I have a variable $data and variable $file in a bash script:

data=$(echo "$(printf '%s\n' "${array[@]/%/$'\n\n'}")") file=$(<scriptfile_results) 

Those variables will contain text. How to compare those two? One option is to use diff(1) utility like this:

diff -u <(echo "$data") <(echo "$file") 

Is this an correct and elegant way to compare content of two variables? In addition how is the <( ) technique called? As I understand, for each <( ) a temporary file(named pipe) is created..

like image 340
Martin Avatar asked Nov 18 '12 03:11

Martin


People also ask

How do I compare two values in bash?

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 = operator with the test [ command. Use the == operator with the [[ command for pattern matching.

How do you compare two variables in Unix?

You can use the [ command (also available as test ) or the [[ … ]] special syntax to compare two variables. Note that you need spaces on the inside of the brackets: the brackets are a separate token in the shell syntax.


1 Answers

Yes, diff <(echo "$foo") <(echo "$bar") is fine.

By searching the bash manpage for the characters <(, you can find that this is called “process substitution.”

You don't need to worry about the efficiency of creating a temporary file, because the temporary file is really just a pipe, not a file on disk. Try this:

$ echo <(echo foo) /dev/fd/63 

This shows that the temporary file is really just the pipe “file descriptor 63.” Although it appears on the virtual /dev filesystem, the disk is never touched.

The actual efficiency issue that you might need to worry about here is the ‘process’ part of “process substitution.” Bash forks another process to perform the echo foo. On some platforms, like Cygwin, this can be very slow if performed frequently. However, on most modern platforms, forking is pretty fast. I just tried doing 1000 process substitutions at once by running the script:

echo <(echo foo) <(echo foo) ... 997 repetitions ... <(echo foo) 

It took 0.225s on my older Mac laptop, and 2.3 seconds in a Ubuntu virtual machine running on the same laptop. Dividing by the 1000 invocations, this shows that process substitutions takes less than 3 milliseconds—something totally dwarfed by the runtime of diff, and probably not anything you need to worry about!

like image 82
andrewdotn Avatar answered Sep 20 '22 08:09

andrewdotn