If I then type echo $?, it displays a 1. I need my code to return 0 unless there is a real error. I have error-handling code elsewhere in the script that catches the error and echoes it to the user:
# Error handling
failure()
{
local lineno=$1
local msg=$2
echo "Line $lineno: $msg"
}
trap 'failure ${LINENO} "$BASH_COMMAND"' ERR
Is there a mistake in my code or is there a workaround to get it to return 0?
By setting -d'' , i.e. the delimiter to the "empty string", read will not stop anywhere but read through to the EOF string which it interprets as end-of-file and sets $? to 1.
Thanks for the reply. Is there a way for me to accomplish the task without generating that return code? I need to put a multi-line string with variable interpolation into a variable.
Use a different delimiter, e.g. a character that definitely won't occur in your text. Good candidates are ASCII control characters. Why not a "carriage return" (<CR> = \r = ^M = 0x0D) which is of inferior significance in *nix texts. Like