Why do speechmarks around an array declaration in bash cause it to have zero length? -
I recently found an irregular error in a Bash script. The code failed to divide by zero error. The script was divided by the length of a declared array, and the array was definitely populated by strings. For some reasons, despite the population, the length of the array was zero. An example script re-presenting the problem is given below.
#! / Bin / bash aCoolFunction () {declared - a message split = "('a' b '' c ')"; Declared - a message not simple Mark = ('A' 'B' 'C'); "$ {MessageSpeechMarks: $ {# messageSpeechMarks [@}}" Length of echo "message length: $ {# message not speechchmark [@]}"} $ {message split mark [@]} "for item" ACoolFunction
Using the GNU Bash 4.3.11 (1) on the Linux Mint, this output returns:
Length of ABC message length of speakers: 0 Length MessageNetwork Mark: 3
As you can see, before the declaration of the array, the speech points out and in the right length array Can anybody explain to me that between declare -a array = (...)
and declare -a array = "(...)"
What is the difference? What does the word really mean with the marks of speech? Thanks
Of course, this Between one should be the difference:
one = "(1 2 3)"
and
declare = In the second case, a
is being set to the cited string (1 2 3)
"(1 2 3)"
So that syntax is important in parentheses And no.
In the declared
statement, on the contrary, someone can expect that the debate will be removed before executing declared
so that Declared
can see brackets as an array marker but on the other hand, it is expected that
declare = (1 2 3)
< / Pre>
a = (1 2 3)
will be treated as a declaration rather than three different arguments.But if the argument is denied, then a
can declare
(orexport
orlocal
) one Scalar variable whose value starts with open brackets?Opacity
bash
indicates difficulty in parsing bilts, especially since there is no standard syntax for guiding us.
Zsh
resolves the problem by not allowing the arrays of assignments in atypeset
announcementksh reported in the OP Works similar to the versions of bash
: it'sa = ...
andtypeset a = ...
in the same way (so both of them In cases preventing quotation marks recognition as an array marker).
Comments
Post a Comment