#!/bin/bash generate_file_to_be_sourced() { local file_to_be_sourced file_to_be_sourced="$(mktemp)" cat << "EOF" > "${file_to_be_sourced}" declare -a tmp='([0]="1" [1]="2")' EOF echo "${file_to_be_sourced}" } foo() { source "${g_file_to_be_sourced}" if declare -p tmp &> /dev/null && [ "${#tmp[@]}" = "2" ] then echo "test #2: OK" else echo "test #2: NOK" fi unset tmp } echo "bash version: ${BASH_VERSION}" echo declare -r g_file_to_be_sourced="$(generate_file_to_be_sourced)" echo "content of file to be sourced:" cat "${g_file_to_be_sourced}" echo source "${g_file_to_be_sourced}" if declare -p tmp &> /dev/null && [ "${#tmp[@]}" = "2" ] then echo "test #1: OK" else echo "test #1: NOK" fi unset tmp foo rm "${g_file_to_be_sourced}"