If you know that result of the command can be error code and this is normal ypu have to use:
echo "------------------------------------------run script without error--------------------------------------------------"
output=$(*docker image rm $(docker image ls -f dangling=true -q) -f* 2>&1) && exit_status=$? || exit_status=$?
echo 'return autput '$output
echo 'return status '$exit_status