Created
August 23, 2013 15:31
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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