Last active
August 29, 2015 13:58
-
-
Save efim-a-efim/9931121 to your computer and use it in GitHub Desktop.
Check if variable/function/array value is declared.
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 | |
is_declared() { | |
# options | |
local _opts='' | |
while getopts ':aAfirtx' _opt; do | |
case "${_opt}" in | |
[aAfirtx]) | |
_opts="${_opts}${_opt}" | |
;; | |
*) | |
return 255 | |
;; | |
esac | |
done | |
[[ "${_opts}" == '-' ]] && _opts='-' | |
shift $((${OPTIND}-1)) | |
# vars | |
while [[ $# -gt 0 ]]; do | |
[[ -z "`declare -${_opts} -p "$1" 2>&1 | grep "^declare \-${_opts} $1"`" ]] && \ | |
return 1 | |
done | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment