In this code :
echo hello > hello.txt
read X <<< $(grep hello hello.txt)
echo $?
$? refers to the exit code of the read statement which is 0. Is there a way to know if the grep failed (e.g., if hello.txt has been deleted by another process) without splitting the read and grep in two statements (i.e., first grep then check $? then read).
read X <<< $()? Why not justX=$()? What part ofreaddo you need?command $(grep hello hello.txt)and then check if thegrepsucceeded.tmp=$(grep hello hello.txt); grep_exit_status=$?; command "$tmp".