Reputation: 2581
I know that there are arrays in bash-scripting. For example:
JOBS=("JOB1", "JOB2", "JOB3")
Then one can refer to, say, JOB2 as follows:
${JOBS[1]}
In a bash script I recently encountered a normal (non-array) variable:
JOB="XYZ"
Later in the script, this variable was referred to as follows:
${JOB[0]}
and:
${JOB[1]}
Since JOB is not an array, I do not understand what ${JOB[number]} is expanded to.
Is this just a programming mistake? Or is there some construct regarding normal variables I am not aware of?
Upvotes: 0
Views: 73
Reputation: 1
I believe newer versions of Bash support one-dimensional arrays and the syntax can be seen in the following link as follows : Advanced Bash-Scripting Guide
If Variable is not an array, ${Variable[Number]} will work only for Number=0 but not for any other number as ${Variable[0]} and $Variable - both are same.
Upvotes: 0
Reputation: 532268
bash
doesn't have array values at all; it provides array syntax to use with names that have an array
attribute set:
$ a=()
$ declare -p a
declare -a a=()
The -a
in the output indicates that the array
attribute is set on a
, having been set implicitly by the preceding array assignment.
For such variables, the name-plus-index acts as a sort of "virtual" name, and like any other name, expands to the empty string if the name doesn't actually exist, as is the case with ${JOB[1]}
. A side effect of the implementation is the $foo
and ${foo[0]}
are generally equivalent, whether or not foo
has its array attribute set.
Upvotes: 3