by
3 8
0
3
1,842
0
Top 1% !
Famous
Specified
Popularity: 13187th place

Published on:

No tags for this snippet yet.
LanguageShell
SourceGitHub

How to check if an a variable is defined as an array in Bash

How to check if an a variable is defined as an array in Bash: 
defined_as_array.sh
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/774274/How-to-check-if-an-a-variable-is-defined?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
foo=" hi declare -a foo" bar=(a b) lint_var_defined_as_non_array() { if [[ "${!1+x}" == "" ]]; then return 1 # not defined fi if [[ "$(declare -p $1)" == "declare -a "* ]]; then return 1 # defined as an array fi return 0 } lint_var_defined_as_array() { if [[ "${!1+x}" == "" ]]; then return 1 # not defined fi if [[ "$(declare -p $1)" == "declare -a "* ]]; then return 0 # defined as an array fi return 1 } if lint_var_defined_as_non_array foo; then echo foo is non array fi if lint_var_defined_as_non_array bar; then echo bar is non array fi if lint_var_defined_as_non_array car; then echo car is non array fi if lint_var_defined_as_array foo; then echo foo is array fi if lint_var_defined_as_array bar; then echo bar is array fi if lint_var_defined_as_array car; then echo car is array fi
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus