Skip to content

Instantly share code, notes, and snippets.

@jmmitchell
Last active June 1, 2018 01:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmmitchell/c4369acb8e9ea1f984541f8819c4c87b to your computer and use it in GitHub Desktop.
Save jmmitchell/c4369acb8e9ea1f984541f8819c4c87b to your computer and use it in GitHub Desktop.
capture standard out, standard error and return code in bash
# #!/bin/bash
#
# based on posts from stackexchange:
# http://stackoverflow.com/a/26827443/171475
# http://stackoverflow.com/a/18086548/171475
# http://stackoverflow.com/a/28796214/171475
function example_function {
printf 'example output to stdout %d\n' {1..10}
echo >&2 'example output to stderr'
return 42
}
##############################
### using the dot operator ###
if [ "${BASH_VERSINFO}" -lt 4 ]; then
printf '%s\n' "The source version of this script requires Bash v4 or higher."
else
# stdout & stderr only
source <({ cmd_err=$({ mapfile -t cmd_out < <(example_function); } 2>&1; declare -p cmd_out >&2); declare -p cmd_err; } 2>&1)
printf "\n%s\n" "SOURCE VERSION : STDOUT & STDERR ONLY"
printf "%s\n" "${cmd_out[@]}"
printf "%s\n" "${cmd_err}"
unset cmd_out
unset cmd_err
# stdout & stderr only as well as return code:
source <({ cmd_err=$({ mapfile -t cmd_out< <( \
example_function \
; cmd_rtn=$?; declare -p cmd_rtn >&3); } 3>&2 2>&1; declare -p cmd_out >&2); declare -p cmd_err; } 2>&1)
printf "\n%s\n" "SOURCE VERSION : STDOUT, STDERR & RETURN CODE"
printf '%s\n' "${cmd_out[@]}"
# alternative version
# declare -p cmd_out
printf '%s\n' "${cmd_err}"
printf '%s\n' "${cmd_rtn}"
unset cmd_out
unset cmd_err
unset cmd_rtn
fi
##############################
######### using exec #########
# stdout & stderr only
eval "$({ cmd_err=$({ cmd_out=$( \
example_function \
); } 2>&1; declare -p cmd_out >&2); declare -p cmd_err; } 2>&1)"
printf "\n%s\n" "EVAL VERSION : STDOUT & STDERR ONLY"
printf '%s\n' "${cmd_out}"
printf '%s\n' "${cmd_err}"
printf '%s\n' "${cmd_rtn}"
unset cmd_out
unset cmd_err
# stdout & stderr only as well as return code:
eval "$({ cmd_err=$({ cmd_out=$( \
example_function \
); cmd_rtn=$?; } 2>&1; declare -p cmd_out cmd_rtn >&2); declare -p cmd_err; } 2>&1)"
printf "\n%s\n" "EVAL VERSION : STDOUT, STDERR & RETURN CODE"
printf '%s\n' "${cmd_out}"
printf '%s\n' "${cmd_err}"
printf '%s\n' "${cmd_rtn}"
unset cmd_out
unset cmd_err
unset cmd_rtn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment