Basically I created a set of tests, that i want to run through, run the test and then compare them with the one's i've got from my before-hand made output files.

So far I've got this

for file in ./tests/*.test; do
# Now I'm unsure how can I get the output of a file stored in a variable.
# I did this, but I'm unsure whether it's correct
./myexec "$file"
# So if my understanding is correct, this should run my desired test and store STDOUT
# Next I want to compare it to what I have inside my output file
compared="$(diff returned ./tests/"$file".output)"
if [ -z $compared ]; then
   echo "Test was successful"
   passed=$((passed + 1))
   echo "Test was unsuccessful, got $ret but expected ./tests/"$file".output")
   failed=$((failed + 1))
# I presume that one above is an incorrect way to print out the expected result
# But I couldn't really think of anything better.

Either way, this is probably fundamentally wrong on multiple levels, but I'm new to shell so this would be pretty helpful for me to improve my understanding

  • The line ... compared="$(diff returned ./tests/"$file".output)" is probably wrong. You probably do not want to execute "diff return ..." Especially since you just set "returned" to $? (which is either going to be 0 or some error code). You probably want "return" to be set to the output "./myexec $file" not the return code. So try return=$(./myexec $file). Dec 5, 2016 at 2:54

1 Answer 1


returned=$? doesn't store STDOUT to returned. It stores the exit code of the last command executed, namely of ./myexec "$file".

Assuming ./tests/"$file".output holds the expected result, what you mean is for example this:

# first assign the value properly using the command substitution
return=$(./myexec $file)

# Then compare using process substitution, as diff demands a file to compare.
## And the process  substitution passes diff the file descriptor of its STDOUT.
compared="$(diff <(echo "$returned") ./tests/"$file".output)" 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .