ranjith
ranjith

Reputation: 53

Using diff to compare a file with a variable

In diff command am getting following error. Kindly assist how can I specify I want to see difference between file ran and variable current_unavail:

$ current_unavail=ranjith
$ cat /tmp/ran
ranjith
$ test=$(cat /tmp/ran)

error which I am getting

$ diff `$current_unavail` `$test`
diff: missing operand after `diff'
diff: Try `diff --help' for more information.

Upvotes: 0

Views: 5308

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74695

You're using the wrong kind of quotes. Assuming that $current_unavail and $test are two shell variables, each containing the name of a file, you should be doing this:

diff "$current_unavail" "$test"

Backticks ` are used for command substitutions (like a=`cmd`), although the preferred syntax is a=$(cmd).


To compare a file /tmp/ran with a variable $current_unavail, you can do this:

diff /tmp/ran <(echo "$current_unavail")

diff works with file descriptors, not variables. But in bash you can use a process substitution <( ... ) to create a temporary file descriptor from the result of executing a command.

Upvotes: 6

Related Questions