Skip to content

Instantly share code, notes, and snippets.

@efim-a-efim
Last active August 29, 2015 13:58
Show Gist options
  • Save efim-a-efim/9931121 to your computer and use it in GitHub Desktop.
Save efim-a-efim/9931121 to your computer and use it in GitHub Desktop.
Check if variable/function/array value is declared.
#!/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