Injected array variables are not available in {bash zxh} shell

This issue is archived. You can view it, but you can't modify it. Learn more

XMLWordPrintable

    Main problem: Injected array variables are not available in shell.

    Injecting variable containing arrays such as the below, fails on bash, zxh and other shells.

     

    test[0]=string1
    test[1]=string2
    foo=bar
    

    Attempting to use these imported variables fails like this:

    >echo ${test[0]}
    >echo ${test[1]}
    >echo ${foo}
    bar

    Normally there are two categories of variables available in shell: shell variables and Environment variables, where shell variables can also be exported to become Environment variables.

    However, when testing the output of five commands that can list variables, it seems that injected array variables falls into a third category, as the array variables are available as Environmental variables, but not as shell variables:

    1. `declare -p`, `declare -xp` and `set` lists no injected array variables
    2. `printenv` and `env` lists injected array variables, as individual entries.

     

    I've implemented workaround, which may be integrated into the plugin, that can be used to fix the main issue. It's available as a gist on github:

    https://gist.github.com/f-steff/1181f8f5907ae9da73779aba8182df3b#file-fixinjectedarraysinbash-sh

    Using this workaround, the output from my test injection above looks like this:

    >. ./FixInjectedArraysInBash.sh
    >echo ${test[0]}
    string1
    >echo ${test[1]}
    string2
    >echo ${foo}
    bar

    Second related problem.

    A warning may be needed when this happens!

    When importing/declaring variables like this:

    test[0]=string1
    test[1]=string2
    test=string3
    

    will result in an unintended overwriting of test[0], resulting in an shell array variable declared like this: 

    • test=([0]="string3" [1]="string2")

     
     

          Assignee:
          Unassigned
          Reporter:
          Flemming Steffensen
          Archiver:
          Jenkins Service Account

            Created:
            Updated:
            Resolved:
            Archived: