Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash unbound variable array (script: s3-bash)

Tags:

bash

I am working with: s3-bash, when I run it in my local environment (OS X 10.10.1) I don't have any problems, when I try to run it on a ubuntu server 14.04.1 I get the following error:

./s3-common-functions: line 66: temporaryFiles: unbound variable
./s3-common-functions: line 85: temporaryFiles: unbound variable

I've looked at the s3-common-functions script and the variable looks to be initialized properly (as an array):

# Globals
declare -a temporaryFiles

But there is a note in the comment, and I'm sure if it's related:

# Do not use this from directly. Due to a bug in bash, array assignments do not work when the function is used with command substitution
function createTemporaryFile
{
    local temporaryFile="$(mktemp "$temporaryDirectory/$$.$1.XXXXXXXX")" || printErrorHelpAndExit "Environment Error: Could not create a temporary file. Please check you /tmp folder permissions allow files and folders to be created and disc space." $invalidEnvironmentExitCode
    local length="${#temporaryFiles[@]}"
    temporaryFiles[$length]="$temporaryFile"
}
like image 514
jordan.baucke Avatar asked Jan 20 '15 21:01

jordan.baucke


People also ask

What does %% mean in bash?

The operator "%" will try to remove the shortest text matching the pattern, while "%%" tries to do it with the longest text matching.

What is unbound variable in shell script?

A symbol that has not been given a value by assignment or in a function call is said to be “unbound.”

What does set Nounset do?

But if you use the set option nounset, which causes the shell to indicate an error when it encounters an undefined variable, then you may be interested in unset. will cause the shell to simply print alice. If the variable is undefined, the shell will print a blank line.


1 Answers

There appears to be a bash behaviour change at play here.

Found by kojiro: CHANGES

hhhh. Fixed a bug that caused `declare' and `test' to find variables that had been given attributes but not assigned values. Such variables are not set.

$ bash --version
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
$ set -u
$ declare -a tF
$ echo "${#tF[@]}"
0

vs.

$ bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ set -u
$ declare -a tF
$ echo "${#tF[@]}"
0

vs.

$ bash --version
GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ set -u
$ declare -a tF
$ echo "${#tF[@]}"
-bash: tF: unbound variable

You can use declare -a tF=() on the newer bash versions to work around this.

$ declare -a tF=()
$ echo "${#tF[@]}"
0
like image 153
Etan Reisner Avatar answered Oct 05 '22 13:10

Etan Reisner