bash:设置数组环境变量并从任何 shell 脚本中取消引用它失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14100409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:07:17  来源:igfitidea点击:

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

linuxbashenvironment-variables

提问by OpenFile

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

我以这种方式将数组设置为环境变量,例如。脚本test.sh

in test.sh

在 test.sh

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

source test.sh

源测试.sh

now in script test-1.sh

现在在脚本中 test-1.sh

#!/bin/bash
echo ${STRING[0]}

the response is nothing, just a blank line, whereas, if I try to set STRING="str1"in test.sh and do echo $STRINGin test-1.sh, this works.

响应什么都不是,只是一个空行,而如果我尝试STRING="str1"在 test.sh 中设置并echo $STRING在 中执行test-1.sh,则此方法有效。

tests are executed from root user only, Now how to set array as env variable , so that I can call the elements of array as per requirement? Earlier, I have tried to even modify /etc/bashrcand that also didn't result in anything positive.

测试仅从 root 用户执行,现在如何将数组设置为 env 变量,以便我可以根据需要调用数组的元素?早些时候,我什至试图修改/etc/bashrc,但也没有产生任何积极的结果。

I need to set the array as env variable as there may be many scripts that i have to write which shall use these variable settings.

我需要将数组设置为 env 变量,因为我可能需要编写许多脚本来使用这些变量设置。

can anybody provide me suggestions to correct me where I am doing wrong?

任何人都可以向我提供建议以纠正我做错的地方吗?

回答by ormaaj

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.

不过,我不知道很多人认为这是一个实际的错误。其他支持 ksh 样式数组的 shell 也不允许导出它们。

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 ""
    typeset ""
    eval "${!1}"
    typeset -p ""
}

typeset -a a=(a b c)
myArr=$(typeset -p a) f myArr a

回答by cdarke

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.

误解在于认为环境变量仅由 shell 使用 - 它们不是。不能将任何属性(包括只读、整数和数组)导出到环境块中。任何语言都可以读取环境变量,C、C++、Perl、Java、Python、PHP 等。它们也存在于 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.

那么,另一种语言如何支持 Bash 特定属性?所有环境变量都转换为字符串,除了在 Bash 中根本不导出数组值。

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

Korn shell 将仅导出第一个元素。ksh93 也做了一些exec利用来保留导出到 Korn shell 子进程的变量属性。

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).

顺便说一句,使用大写作为变量名被认为是不好的做法,因为它们可能与 shell 使用的那些发生冲突。此外,在 Bash 3 上,名称 STRING 在导出时存在问题(在 Bash 4 中已修复)。

回答by Toby Speight

The environment variables passed from processes to their children are unstructured strings; arrays cannot be supported. You can demonstrate this in Bash:

从进程传递给其子进程的环境变量是非结构化字符串;不支持数组。您可以在 Bash 中演示这一点:

export x=foo
printenv x

That outputs foo. If I now cause xto become an array

那输出foo. 如果我现在导致x成为一个数组

x=(foo bar)
printenv x

We see no output (xis not exported).

我们看不到输出(x未导出)。

回答by Thuy Dang

Thissays passing array as env variable is possible with bash script though. It works for defining and dereferencing the array in bash command, but it does not work when dereferencing from bash script (ubuntu 18.04).

表示使用 bash 脚本可以将数组作为 env 变量传递。它适用于在 bash 命令中定义和取消引用数组,但在从 bash 脚本(ubuntu 18.04)取消引用时不起作用。

回答by Ignacio Vazquez-Abrams

You're trying to put an array into an environment variable, and environment variables can only be strings. bash doesn't have a method for properly serializing/deserializing arrays; do so manually.

您试图将数组放入环境变量中,而环境变量只能是字符串。bash 没有正确序列化/反序列化数组的方法;手动执行此操作。