bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

sourcing script file from inside a function doesn't work anymore


From: thioroup8
Subject: sourcing script file from inside a function doesn't work anymore
Date: Sat, 10 May 2014 12:22:40 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu' -DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I../. -I.././include -I.././lib -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wall uname output: Linux thioroup8-laptop 3.13.0-26-generic #48-Ubuntu SMP Wed May 7 23:31:02 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 4.3
Patch Level: 11
Release Status: release

Description:
I write a bash script file containing variables definitions.
From another bash script file, I source it from outside any function: in this case all is working fine as expected.
Now, I source it from inside a function: I obtain following error message:
bash: declare: tmp: not found
I think this problem raises in the particular case of involving array variables definitions inside sourced bash script file...

Repeat-By:
You can execute the script below:
#!/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}"

If you need further informations, don't hesitate to ask me...

Best regards
Jean-Philippe




reply via email to

[Prev in Thread] Current Thread [Next in Thread]