2

While the solution suggested in Bash empty array expansion with `set -u` worked great for me, turns out array handling has been changed in recently released (2016/09/16) bash 4.4 (available in Debian stretch, for example).

$ bash --version | head -n1
bash --version | head -n1
GNU bash, version 4.4.0(1)-release (x86_64-pc-linux-gnu)

Now empty arrays expansion does not emits warning

$ set -u
$ arr=()
$ echo "${arr[@]}"

$ # everything is fine

The solution proposed in related question fails with bash-4.4:

$ set -u
$ arr2=()
$ arr2=( ${arr2[@] + "${arr2[@]}"} 'foo' )
bash: ${arr2[@] + "$arr2[@]"}: bad substitution

Does anyone got suggestions on (more or less) version-independent solution without extra checks for array length or bash version ? I'm still on investigating latest bash changes myself

EDIT

As my initial question seems to be somewhat confusing, here is clarification of what I try to achieve. Test script:

#!/usr/bin/env bash
set -euo pipefail
bash --version | head -n1

arr=()

# some external environment variables are set / command line arguments passed to script
if test -n "${USE_EXTRA_ARGUMENT_1-}" ; then
  arr=( ${arr[@] + "${arr[@]}"} 'foo' )
fi

if test -n "${USE_EXTRA_ARGUMENT_2-}" ; then
  arr=( ${arr[@] + "${arr[@]}"} 'bar' )
fi

# just a dummy command
echo "${arr[@]-}"

Bash 4.3 (Arch linux) run:

$ USE_EXTRA_ARGUMENT_1=baz bash xxx.sh 
GNU bash, version 4.3.46(1)-release (x86_64-unknown-linux-gnu)
foo

Bash 4.4 (Debian stretch) run:

$ USE_EXTRA_ARGUMENT_1=baz bash xxx.sh
GNU bash, version 4.4.0(1)-release (x86_64-pc-linux-gnu)
xxx.sh: line 9: ${arr[@] + "${arr[@]}"}: bad substitution

Or I'm gravely wrong in using bash arrays ?

Community
  • 1
  • 1
agg3l
  • 1,444
  • 15
  • 21

1 Answers1

6

The space before + is wrong; you mean

arr2=( ${arr2[@]+"${arr2[@]}"} 'foo' )

It's much simpler, though, to use the += operator.

arr2+=(foo)
chepner
  • 497,756
  • 71
  • 530
  • 681