1

I've recently updated my system and got a new bash version. Since then I've encountered some nasty behaviour of my bash scripts, which I finally managed to track down to a new behaviour of bash's declare / local commands.

Considering the following minimal working example:

#!/bin/bash

function printarray1 () {
  local -a arr=("${!1}")
  echo "${arr[@]}" # printing the complete array
  echo "${arr[9]}" # just to check if it is really recognized as an integer indexed array
}

function printarray2 () {
  local arr=("${!1}")
  echo "${arr[@]}" # printing the complete array
  echo "${arr[9]}" # just to check if it is really recognized as an integer indexed array
}

arr=("01" "02" "03" "04" "05" "06" "07" "08" "09" "10")
echo "Declaration as indexed array:"
printarray1 arr[@]
echo "Undefined declaration:"
printarray2 arr[@]

On GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu) this results in

Declaration as indexed array:
01 02 03 04 05 06 07 08 09 10
10
Undefined declaration:
01 02 03 04 05 06 07 08 09 10
10

while the newer GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu) version returns

Declaration as indexed array:

Undefined declaration:
01 02 03 04 05 06 07 08 09 10
10

Note that the behaviour is the same when I use "declare" instead of "local".

I could not find anything about a change of the declare options in Bash 4.3. The help (help declare) is equal in both versions for all relevant information. I've even stumbled upon the claim that "All variables can be used as arrays without explicit definition." (see Why are "declare -f" and "declare -a" needed in bash scripts?).

Can anyone explain this behaviour? Is it a new feature? Or simply a bug? Has the passing of arrays to functions been restricted? To me it's pretty scary when the bash behaviour suddenly changes from version to version.

9
  • The problem is the variable name collision. Use a different global variable and the problem goes away. Oct 27, 2015 at 15:01
  • With another variable name it works indeed. But that doesn't really explain the different behaviour between Bash 4.2 and 4.3, nor declare -a and declare. And I assumed the sense behind the local keyword has been to hide variables from the local scope.
    – loli
    Oct 27, 2015 at 15:13
  • Yeah. I didn't see anything in a quick look at the notes that would explain it and I would have expected it to break completely in the newer version and not just partly. I feel like I've seen a question about this before I'll have to go looking for it in a bit. Oct 27, 2015 at 15:51
  • @EtanReisner You are probably remembering my question a few months ago although that was to do with a bug in bash 3.1.
    – 123
    Oct 27, 2015 at 15:54
  • @123 Almost certainly correct. Thanks. Don't have time to see if something similar applies here too at the moment though. Oct 27, 2015 at 15:57

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.