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

前端 未结 5 1998
余生分开走
余生分开走 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:37

    The misunderstanding is in thinking that environment variables are only used by shells - they are not. No attributes, including readonly, integer, and arrays, can be exported into the environment block. Environment variables may be read by any language, C, C++, Perl, Java, Python, PHP, and so on. They also exist on Windows.

    So, how could another language support Bash specific attributes? All environment variables are converted to strings, except in Bash where array values are not exported at all.

    Korn shell will export just the first element. ksh93 also does some exec exploitation to preserve variable attributes exported to Korn shell children.

    By the way, it is considered bad practice to use UPPERCASE for variable names, since they could collide with those used by the shell. Also, on Bash 3, the name STRING has issues when exported (fixed in Bash 4).

提交回复
热议问题