Your command
diff `python3 program.py > redirect_file.txt` compare_file.txt
does not work because of incorrect use of backticks. Backticks differ from double quotes just in that their contents is evaluated by shell and replaced by its standard output. Because you redirected the command’s standard output to a file, backticks now evaluate to empty string. Thus your command is equivalent to:
python3 program.py > redirect_file.txt
diff "" compare_file.txt
But you want:
python3 program.py > redirect_file.txt
diff redirect_file.txt compare_file.txt
If the redirect_file.txt
is used just for the diff, you can avoid creating it and speed up the process:
python3 program.py | diff - compare_file.txt
This uses pipe (|
), which basically connects standard output of the command on the left to standard input of the command on the right. Diff reads standard input when -
is given instead of actual file name, which is a pretty common convention among shell utilities.
You could also use Bash-specific syntax
diff <(python3 program.py) compare_file.txt
but this is not as portable and creates a named pipe, which is unnecessary and potential source of trouble.