Skip to content

Instantly share code, notes, and snippets.

@habibutsu
Created May 21, 2013 10:07
Show Gist options
  • Save habibutsu/5618756 to your computer and use it in GitHub Desktop.
Save habibutsu/5618756 to your computer and use it in GitHub Desktop.
Execute command in bash with copy stderr into variable and stdout into stdout of script
function exec_cmd {
exec 3>&1
error=$( { $@ 1>&3; } 2>&1 )
exec 3>&-
if [ "${error}" ]; then
echo "[ERROR] ${error}"
exit 1
fi
}
# Example usage
exec_cmd ls -la
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment