Skip to content

Instantly share code, notes, and snippets.

@zvyn
Created August 23, 2013 15:31
Show Gist options
  • Save zvyn/6320645 to your computer and use it in GitHub Desktop.
Save zvyn/6320645 to your computer and use it in GitHub Desktop.
Store multiple values in a declarative array, including the error-code of a command. (Note that in most cases you want to use $? directly or $PIPESTATUS if you have a pipe to get just the error-code.) Needs bash-version > 4.
#!/bin/bash
# generates a string in the form of an array-declaration and puts it into declare -A
declare -A exit_status=$(echo -n "( \
[output]=\"" ; cat /no/such/file 2>&1 ; echo -en "\"
[errno]=$?
)")
# to test it:
echo error ${exit_status[errno]}: ${exit_status[output]} >/dev/stderr
exit ${exit_status[errno]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment