I want to test if the output of a function matches the text I envision it should write to standard output. But the code below never prints test PASS and I can't figure out why. I also tried putting various \n characters or deleting them in the variable GROUND_TRUTH but that didn't help.
error_out()
{
printf "%s\n" "${ERROR}"
printf "Return to exit\n"
read -r THROW_AWAY
exit
}
ERROR="Test output."
OUTPUT_TO_TEST=$(error_out)
GROUND_TRUTH="Test output.\nReturn to exit\n"
{ [ "${OUTPUT_TO_TEST}" = "${GROUND_TRUTH}" ] \
&& printf "error_out test PASS\n"; } \
|| { \
printf "error_out test FAIL\n"; \
}
"\n"should be in fact aLF. Also, when capturing the output of a command, the trailingLF(s) are trimmed soGROUND_TRUTHshouldn't end with aLF... BTW, what's the purpose ofread -r THROW_AWAY? Waiting the the user to type ENTER?GROUND_TRUTH="Test output.\nReturn to exit"and it doesn't work. And the purpose ofread -r THROW_AWAYis that the user should see the message in theERRORstring variable.GROUND_TRUTH="Test output.(and in following line)return to exit"GROUND_TRUTH=$'Test output.\nReturn to exit'PATH=somethingand then wonder why your script is broken: (1) (2) (3).