Q:

Print the last error message from stderr?

I have a script which deploys a web service, and I found this little helper to exitWithMessageOnError. 
exitWithMessageOnError () {
  if [ ! $? -eq 0 ]; then
    echo "An error has occurred during web site deployment."
    echo $1
    exit 1
  fi
}

This is currently running because grunt is failing for some reason. 
grunt --no-color --verbose clean build
exitWithMessageOnError "grunt failed"

Unfortunately all I get back is An error has occurred during web site deployment. and grunt failed. What I would like to do is print the error that occurred so I can fix it. 
I tried something with >&2 but I wasn't able to get it working. 

A:

If a program alone does write to stderr you should be able to see its error output without the need for that helper function. In your post your command suggests you didn't use any redirection so you were supposed to see the error stream. To troubleshoot you of course could redirect both stdout and stderr  to two separate files, (or both to the same one with &> file.txt ) and see what is going on actually

