|
echo "## do not resolve references" |
|
echo |
|
|
|
echo "# string" |
|
str="Hello world!" |
|
echo "declaration: $(declare -p str)" |
|
echo "typeof : $(typeof -f str)" |
|
echo |
|
|
|
echo "# array" |
|
arr=(foo bar baz) |
|
echo "declaration: $(declare -p arr)" |
|
echo "typeof : $(typeof -f arr)" |
|
echo |
|
|
|
echo "# associative array" |
|
declare -A map=([foo]="tomcat" [bar]="kitty" [baz]="kitten") |
|
echo "declaration: $(declare -p map)" |
|
echo "typeof : $(typeof -f map)" |
|
echo |
|
|
|
echo "# function" |
|
fn() { true; } |
|
echo "declaration: $(declare -f fn | xargs printf "%s")" |
|
echo "typeof : $(typeof -f fn)" |
|
echo |
|
|
|
echo "# undefined" |
|
echo "declaration: n/a" |
|
echo "typeof : $(typeof -f nonexistent)" |
|
echo |
|
|
|
echo "# reference" |
|
declare -n ref="str" |
|
echo "declaration: $(declare -p ref)" |
|
echo "typeof : $(typeof -f ref)" |
|
echo |
|
|
|
echo "# undefined reference" |
|
declare -n ref="undefined" |
|
echo "declaration: $(declare -p ref)" |
|
echo "typeof : $(typeof -f ref)" |
|
echo |
|
|
|
|
|
echo "## resolve references" |
|
echo |
|
|
|
echo "# resolve: undefined reference" |
|
declare -n ref="undefined" |
|
echo "declaration: $(declare -p ref)" |
|
echo "typeof : $(typeof ref)" |
|
echo |
|
|
|
echo "# resolve: reference to string" |
|
declare -n ref="str" |
|
echo "declaration: $(declare -p ref)" |
|
echo "typeof : $(typeof ref)" |
|
echo |
|
|
|
echo "# resolve: reference to array" |
|
declare -n ref="arr" |
|
echo "declaration: $(declare -p ref)" |
|
echo "typeof : $(typeof ref)" |
|
echo |
|
|
|
echo "# resolve: reference to associative array" |
|
declare -n ref="map" |
|
echo "declaration: $(declare -p ref)" |
|
echo "typeof : $(typeof ref)" |
|
echo |
|
|
|
echo "## recursion" |
|
echo |
|
|
|
echo "# resolve: reference to reference to string" |
|
declare -n ref2="str" |
|
declare -n ref="ref2" |
|
echo "declaration: $(declare -p ref)" |
|
echo "typeof : $(typeof ref)" |
|
echo |
|
|
|
echo "# resolve: reference to reference to undefined" |
|
declare -n ref2="undefined" |
|
declare -n ref="ref2" |
|
echo "declaration: $(declare -p ref)" |
|
echo "typeof : $(typeof ref)" |
|
echo |
|
|
|
echo "## special test cases" |
|
echo |
|
|
|
echo "# reference to function" |
|
# TODO: function can be declared by `declare` but does it make sense checking for it? |
|
declare -n ref="fn" |
|
echo "declaration: $(declare -p ref)" |
|
echo "typeof : $(typeof ref)" |
|
echo |
|
|
|
echo "# self-feference" |
|
echo "covered by $(declare -n ref="ref" 2>&1 || true)" |
|
echo |
|
|
|
echo "# global declaration with export (multiple args in declare command)" |
|
declare -agx arr2=(foo bar) |
|
echo "declaration: $(declare -p arr2)" |
|
echo "typeof : $(typeof arr2)" |
|
echo |
|
|
|
echo "# global declaration with export (multiple args in declare command)" |
|
echo "declaration: $(declare -p arr2)" |
|
echo "typeof : $(typeof arr2)" |
|
echo |
|
|
|
echo "# associative array passed to inside function" |
|
inside_function_typeof() { local -n my_map="$1"; typeof my_map; } |
|
inside_function_declare() { local -n my_map="$1"; declare -p my_map; } |
|
inside_function_print_keys() { local -n my_map="$1"; echo "${!my_map[@]}"; } |
|
echo "declaration: $(declare -f inside_function_declare)" |
|
echo "inside typeof : $(inside_function_typeof map)" |
|
echo "inside declaration: $(inside_function_declare map)" |
|
echo "inside print : $(inside_function_print_keys map)" |
|
echo |