bash: set array env variable and de-referencing it from any shell script fails

前端 未结 5 1991
余生分开走
余生分开走 2020-12-04 01:49

I set the a array as an environment variable in this manner eg. script test.sh

in test.sh

#!/bin/bash
export STRING=( \"str1\" \"str2\"         


        
5条回答
  •  失恋的感觉
    2020-12-04 02:39

    Read the fine manual, "bugs" section.

    Array variables may not (yet) be exported.

    Though, I don't know that many consider this an actual bug. Other shells that support ksh-style arrays don't allow exporting them either.

    You may pass around array definitions rather easily, through parameters or variables or the environment. It isn't usually very useful though.

    function f {
        unset -v "$2"
        typeset "$2"
        eval "${!1}"
        typeset -p "$2"
    }
    
    typeset -a a=(a b c)
    myArr=$(typeset -p a) f myArr a
    

提交回复
热议问题