在 bash 脚本中导出数组
我无法将数组从 bash 脚本导出到另一个 bash 脚本,如下所示:
export myArray[0]="Hello"
export myArray[1]="World"
当我这样写时,没有问题:
export myArray=("Hello" "World")
由于多种原因,我需要将数组初始化为多行。你有什么解决办法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
来自 ubuntu 10.04 下 bash 版本 4.1.5 的联机帮助页。
Chet Ramey(2011 年现任 bash 维护者)的以下声明可能是有关此“bug”的最官方文档:
http://www.mail-archive.com/[电子邮件受保护]/msg01774.html
From the manpage of bash version 4.1.5 under ubuntu 10.04.
The following statement from Chet Ramey (current bash maintainer as of 2011) is probably the most official documentation about this "bug":
http://www.mail-archive.com/[email protected]/msg01774.html
TL;DR:可导出数组在 bash-5.1 及之前版本均不直接受支持,但您可以通过以下两种方式之一(有效)导出数组:
或者,您可以等到bash-4.3发布(正在开发中/截至 2014 年 2 月的 RC 状态,请参阅变更日志中的 ARRAY_EXPORT)。更新:此功能在 4.3 中未启用。如果在构建时定义了 ARRAY_EXPORT ,构建将会失败。作者已声明不打算完成此任务特征。首先要了解的是 bash 环境(更准确地说 命令执行环境)与 POSIX 环境概念不同。 POSIX 环境 是非类型化
name= 的集合值
对,并且可以以各种方式从进程传递给其子进程方式(实际上是IPC的有限形式)。bash 执行环境实际上是其超集,具有类型化变量、只读和可导出标志、数组、函数等。这部分解释了为什么
set
(bash 内置)和env
或printenv
的输出不同。当您调用另一个bash shell时,您正在启动一个新进程,您会丢失一些bash状态。但是,如果您对脚本进行点源,则该脚本将在同一环境中运行;或者,如果您通过
( )
运行子 shell,环境也会被保留(因为 bash 分叉,保留其完整状态,而不是使用进程环境重新初始化)。@lesmana 的答案中提到的限制是因为 POSIX 环境只是
name=value
对,没有额外的含义,因此没有一致的方法来编码或格式化类型变量,请参阅下面的一个有趣的 bash 怪癖函数,以及 bash-4.3 中即将发生的更改(提议的数组功能已放弃)。有几种简单的方法可以使用
declare -p
(内置)将一些 bash 环境输出为一组一个或多个declare
语句,这些语句可用于重建“名称”的类型和值。这是基本的序列化,但是复杂性其他一些答案暗示。 declare -p 保留数组索引、稀疏数组和麻烦值的引用。对于数组的简单序列化,您可以逐行转储值,然后使用 read -a myarray 来恢复它(适用于连续的 0 索引数组,因为 read -a ) code> 自动分配索引)。这些方法不需要对您将数组传递到的脚本进行任何修改。
上述
bash -c "..."
形式的变体有时会在 crontab 中(错误地)使用来设置变量。替代方案包括:
或者,作为单行:
最后一个使用进程替换将
declare
命令的输出作为rc脚本传递。 (此方法仅适用于 bash-4.0 或更高版本:早期版本无条件fstat()
rc 文件并使用返回的大小一次性返回到read()
文件; FIFO 返回大小 0,因此不会按预期工作。)在非交互式 shell(即 shell 脚本)中,
BASH_ENV
变量指向的文件自动获取。您必须确保 bash 被正确调用,可能使用 shebang 显式调用“bash”,而不是#!/bin/sh
,因为 bash 在进入时不会遵循BASH_ENV
历史/POSIX 模式。如果所有数组名称碰巧都有一个公共前缀,您可以使用 declare -p ${!myprefix*} 来展开它们的列表,而不是枚举它们。
您可能不应该尝试使用此方法导出和重新导入整个 bash 环境,一些特殊的 bash 变量和数组是只读的,并且修改特殊变量时可能会产生其他副作用。
您也可以通过将数组定义序列化为可导出变量并使用
eval
来做一些稍微令人不快的事情,但我们不鼓励使用eval
...)
( 上面,有一个有趣的怪癖:通过环境导出函数的特殊支持:
使用
export -f
或set +a
启用此行为,将导致 ( process) 环境,通过printenv
可见:变量为
functionname
(或functioname()
以实现向后兼容),其值为( ) { 函数体 }
。当后续的 bash 进程启动时,它将从每个此类环境变量重新创建一个函数。如果您查看 bash-4.2 源文件
variables.c
,您将看到以() {
开头的变量经过特殊处理。 (尽管禁止使用此语法与declare -f
创建函数。)更新:“shellshock" 安全问题与此功能相关,当代系统可能会禁用从环境中自动导入函数作为缓解措施。如果您继续阅读,您将看到一个
#if 0
(或#if ARRAY_EXPORT
)保护代码,用于检查以([
并以)
结尾,并附有注释“数组变量可能尚未导出”。好消息是,在当前的开发版本 bash-4.3rc2 中,导出索引数组(非关联)的功能已启用。该功能不太可能启用,如上所述。我们可以使用它来创建一个函数来恢复所需的任何数组数据:
因此,与之前的方法类似,调用子脚本:
或者,您可以有条件地调用子脚本中的
sharearray
函数,方法是:在某个适当的点添加:请注意,
sharearray
函数中没有declare -a
,如果您这样做,则该数组隐式本地功能,而不是想要的。 bash-4.2 支持declare -g
,将函数中声明的变量变成全局变量,因此可以使用declare -ga
。 (由于关联数组需要一个declare -A
,因此您将无法在 bash-4.2 之前的版本(从 v4.2 开始)对全局关联数组使用此方法声明 -Ag
将按希望工作。)GNUparallel
文档对此方法有有用的变体,请参阅 --env 的讨论="http://www.gnu.org/software/parallel/man.html" rel="noreferrer">手册页。您的问题的措辞还表明您可能在
export
本身方面遇到问题。您可以在创建或修改名称后将其导出。 “可导出”是变量的标志或属性,为了方便起见,您也可以在单个语句中设置和导出。直到 bash-4.2export
只需要一个名称,支持简单(标量)变量或函数名称。即使您(将来)可以导出数组,也可能不支持导出选定的索引(切片)(尽管由于数组稀疏,所以没有理由不允许它)。尽管 bash 也支持语法
declare -a name[0]
,但下标会被忽略,“name”只是一个普通的索引数组。TL;DR: exportable arrays are not directly supported up to and including bash-5.1, but you can (effectively) export arrays in one of two ways:
Or, you can wait until bash-4.3 is released (in development/RC state as of February 2014, see ARRAY_EXPORT in the Changelog).Update: This feature is not enabled in 4.3. If you defineARRAY_EXPORT
when building, the build will fail. The author has stated it is not planned to complete this feature.The first thing to understand is that the bash environment (more properly command execution environment) is different to the POSIX concept of an environment. The POSIX environment is a collection of un-typed
name=value
pairs, and can be passed from a process to its children in various ways (effectively a limited form of IPC).The bash execution environment is effectively a superset of this, with typed variables, read-only and exportable flags, arrays, functions and more. This partly explains why the output of
set
(bash builtin) andenv
orprintenv
differ.When you invoke another bash shell you're starting a new process, you loose some bash state. However, if you dot-source a script, the script is run in the same environment; or if you run a subshell via
( )
the environment is also preserved (because bash forks, preserving its complete state, rather than reinitialising using the process environment).The limitation referenced in @lesmana's answer arises because the POSIX environment is simply
name=value
pairs with no extra meaning, so there's no agreed way to encode or format typed variables, see below for an interesting bash quirk regarding functions, and an upcoming change in bash-4.3(proposed array feature abandoned).There are a couple of simple ways to do this using
declare -p
(built-in) to output some of the bash environment as a set of one or moredeclare
statements which can be used reconstruct the type and value of a "name". This is basic serialisation, but with rather less of the complexity some of the other answers imply.declare -p
preserves array indexes, sparse arrays and quoting of troublesome values. For simple serialisation of an array you could just dump the values line by line, and useread -a myarray
to restore it (works with contiguous 0-indexed arrays, sinceread -a
automatically assigns indexes).These methods do not require any modification of the script(s) you are passing the arrays to.
Variations on the above
bash -c "..."
form are sometimes (mis-)used in crontabs to set variables.Alternatives include:
Or, as a one-liner:
The last one uses process substitution to pass the output of the
declare
command as an rc script. (This method only works in bash-4.0 or later: earlier versions unconditionallyfstat()
rc files and use the size returned toread()
the file in one go; a FIFO returns a size of 0, and so won't work as hoped.)In a non-interactive shell (i.e. shell script) the file pointed to by the
BASH_ENV
variable is automatically sourced. You must make sure bash is correctly invoked, possibly using a shebang to invoke "bash" explicitly, and not#!/bin/sh
as bash will not honourBASH_ENV
when in historical/POSIX mode.If all your array names happen to have a common prefix you can use
declare -p ${!myprefix*}
to expand a list of them, instead of enumerating them.You probably should not attempt to export and re-import the entire bash environment using this method, some special bash variables and arrays are read-only, and there can be other side-effects when modifying special variables.
(You could also do something slightly disagreeable by serialising the array definition to an exportable variable, and using
eval
, but let's not encourage the use ofeval
...)
As referenced above, there's an interesting quirk: special support for exporting functions through the environment:
with
export -f
orset +a
to enable this behaviour, will result in this in the (process) environment, visible withprintenv
:The variable is
functionname
(orfunctioname()
for backward compatibility) and its value is() { functionbody }
.When a subsequent bash process starts it will recreate a function from each such environment variable. If you peek into the bash-4.2 source file
variables.c
you'll see variables starting with() {
are handled specially. (Though creating a function using this syntax withdeclare -f
is forbidden.) Update: The "shellshock" security issue is related to this feature, contemporary systems may disable automatic function import from the environment as a mitigation.If you keep reading though, you'll see an
#if 0
(or#if ARRAY_EXPORT
) guarding code that checks variables starting with([
and ending with)
, and a comment stating "Array variables may not yet be exported".The good news is that in the current development version bash-4.3rc2 the ability to export indexed arrays (not associative) is enabled.This feature is not likely to be enabled, as noted above.We can use this to create a function which restores any array data required:
So, similar to the previous approach, invoke the child script with:
Or, you can conditionally invoke the
sharearray
function in the child script by adding at some appropriate point:Note there is no
declare -a
in thesharearray
function, if you do that the array is implicitly local to the function, not what is wanted. bash-4.2 supportsdeclare -g
that makes a variable declared in a function into a global, sodeclare -ga
can then be used. (Since associative arrays require adeclare -A
you won't be able to use this method for global associative arrays prior to bash-4.2, from v4.2declare -Ag
will work as hoped.) The GNUparallel
documentation has useful variation on this method, see the discussion of--env
in the man page.Your question as phrased also indicates you may be having problems with
export
itself. You can export a name after you've created or modified it. "exportable" is a flag or property of a variable, for convenience you can also set and export in a single statement. Up to bash-4.2export
expects only a name, either a simple (scalar) variable or function name are supported.Even if you could (in future) export arrays, exporting selected indexes (a slice) may not be supported (though since arrays are sparse there's no reason it could not be allowed). Though bash also supports the syntax
declare -a name[0]
, the subscript is ignored, and "name" is simply a normal indexed array.天啊。我不知道为什么其他答案让这个变得如此复杂。 Bash 几乎内置了对此的支持。
在导出脚本中:(
然后以下内容将重新构建数组:
注意!如果您不能信任
\n''\nbaz)' ) # an array with two nasty elements myArray="${myArray[@]@Q}" ./importing_script.shmyArrayeval
代码>环境变量。这个技巧展示了“Little Bobby Tables”漏洞。想象一下,如果有人将myArray
的值设置为) ; rm -rf / #
。注意,双引号对于正确处理数组元素中的空格是必需的。)
进入
importing_script.sh
时,myArray
环境的值变量包含这些确切的 26 个字节:然后以下内容将重新构建数组:
注意!如果您不能信任
myArrayeval
代码>环境变量。这个技巧展示了“Little Bobby Tables”漏洞。想象一下,如果有人将myArray
的值设置为) ; rm -rf / #
。Jeez. I don't know why the other answers made this so complicated. Bash has nearly built-in support for this.
In the exporting script:
Then the following will reconstitute the array:
CAUTION! Do not
\n''\nbaz)' ) # an array with two nasty elements myArray="${myArray[@]@Q}" ./importing_script.sheval
like this if you cannot trust the source of themyArray
environment variable. This trick exhibits the "Little Bobby Tables" vulnerability. Imagine if someone were to set the value ofmyArray
to) ; rm -rf / #
.(Note, the double quotes are necessary for correct handling of whitespace within array elements.)
Upon entry to
importing_script.sh
, the value of themyArray
environment variable comprises these exact 26 bytes:Then the following will reconstitute the array:
CAUTION! Do not
eval
like this if you cannot trust the source of themyArray
environment variable. This trick exhibits the "Little Bobby Tables" vulnerability. Imagine if someone were to set the value ofmyArray
to) ; rm -rf / #
.环境只是键值对的集合,它们都是字符串。适用于任何类型数组的正确解决方案可以将
这些内容在其他帖子中都有介绍。如果您知道您的值从不包含特定字符(例如
|
)并且您的键是连续整数,您可以简单地将数组保存为分隔列表:并在子进程中恢复它:
The environment is just a collection of key-value pairs, both of which are character strings. A proper solution that works for any kind of array could either
These are covered in the other posts. If you know that your values never contain a certain character (for example
|
) and your keys are consecutive integers, you can simply save the array as a delimited list:And restore it in the child process:
基于 @mr.spuratic 对
BASH_ENV
的使用,这里我通过script -f -c
script -c
可用于在另一个 pty(和进程组)内运行命令,但它不能将任何结构化参数传递给$@
隧道命令>
。相反,
是一个简单的字符串,作为system
库调用的参数。我需要将外部 bash 的
$@
隧道传输到脚本调用的 bash 的$@
中。由于
declare -p
不能采用@
,这里我使用魔法 bash 变量_
(带有一个虚拟的第一个数组值,因为它将被覆盖bash)。这使我不必践踏任何重要的变量:概念证明:
BASH_ENV=<( 声明 -a _=("" "$@") && 声明 -p _ ) bash -c 'set -- "${_[@]:1}" & & echo "$@"'
“但是,”你说,“你正在向 bash 传递参数——事实上我也是如此,但这些是一个已知字符的简单字符串。这里是
script< 的使用/code>
SHELL=/bin/bash BASH_ENV=<( 声明 -a _=("" "$@") && 声明 -p _ && echo 'set -- "${ _[@]:1}"') script -f -c 'echo "$@"' /tmp/logfile
它给了我这个包装函数
in_pty
:in_pty( ){
SHELL=/bin/bash BASH_ENV=<( 声明 -a _=("" "$@") && 声明 -p _ && echo 'set -- "${_[@]:1 }"') script -f -c 'echo "$@"' /tmp/logfile
}
或将此无函数包装器作为 Makefile 的可组合字符串:
in_pty=bash -c 'SHELL=/bin/bash BASH_ENV=<(声明 -a _=("" "$$@ ") && 声明 -p _ && echo '"'"'set -- "$${_[@]:1}"'"'"') script -qfc '"'"'" $$@"'"'"' /tmp/logfile' --
...
$(in_pty) test --verbose $@ $^
Based on @mr.spuratic use of
BASH_ENV
, here I tunnel$@
throughscript -f -c
script -c <command> <logfile>
can be used to run a command inside another pty (and process group) but it cannot pass any structured arguments to<command>
.Instead
<command>
is a simple string to be an argument to thesystem
library call.I need to tunnel
$@
of the outer bash into$@
of the bash invoked by script.As
declare -p
cannot take@
, here I use the magic bash variable_
(with a dummy first array value as that will get overwritten by bash). This saves me trampling on any important variables:Proof of concept:
BASH_ENV=<( declare -a _=("" "$@") && declare -p _ ) bash -c 'set -- "${_[@]:1}" && echo "$@"'
"But," you say, "you are passing arguments to bash -- and indeed I am, but these are a simple string of known character. Here is use by
script
SHELL=/bin/bash BASH_ENV=<( declare -a _=("" "$@") && declare -p _ && echo 'set -- "${_[@]:1}"') script -f -c 'echo "$@"' /tmp/logfile
which gives me this wrapper function
in_pty
:in_pty() {
SHELL=/bin/bash BASH_ENV=<( declare -a _=("" "$@") && declare -p _ && echo 'set -- "${_[@]:1}"') script -f -c 'echo "$@"' /tmp/logfile
}
or this function-less wrapper as a composable string for Makefiles:
in_pty=bash -c 'SHELL=/bin/bash BASH_ENV=<( declare -a _=("" "$$@") && declare -p _ && echo '"'"'set -- "$${_[@]:1}"'"'"') script -qfc '"'"'"$$@"'"'"' /tmp/logfile' --
...
$(in_pty) test --verbose $@ $^
我正在编辑另一篇文章并犯了一个错误。啊啊。无论如何,也许这可能有帮助?
https://stackoverflow.com/a/11944320/1594168
请注意,因为 shell 的数组格式在 bash 或任何另一个壳的侧面,
以与平台无关的方式返回 shell 数组是非常困难的。
您必须检查版本,并制作一个简单的脚本来连接所有内容
shell 数组转换成其他进程可以解析的文件。
但是,如果您知道要带回家的数组的名称,那么还有一种方法,虽然有点脏。
可以说我有
所以我想把它带回家
我们可以通过从子进程检查
结果来看到它正在导出:
如果我们绝对必须,请使用环境变量来转储它。
然后
在运行另一个脚本之前,
I was editing a different post and made a mistake. Augh. Anyway, perhaps this might help?
https://stackoverflow.com/a/11944320/1594168
Note that because the shell's array format is undocumented on bash or any other shell's side,
it is very difficult to return a shell array in platform independent way.
You would have to check the version, and also craft a simple script that concatinates all
shell arrays into a file that other processes can resolve into.
However, if you know the name of the array you want to take back home then there is a way, while a bit dirty.
Lets say I have
So I want to take it home
We can see it being exported, by checking from a sub-process
Result :
If we absolutely must, use the env var to dump it.
Then
Before running the another script,
正如 lesmana 所报告的,您无法导出数组。所以你必须在通过环境之前将它们序列化。这种序列化在其他只适合字符串的地方也很有用(su -c 'string'、ssh host 'string')。执行此操作的最短代码方法是滥用“getopt”,
如下使用:
这不会解决未设置/稀疏数组的问题。
您也许可以减少restore_array 中的2 次“eval”调用。
As lesmana reported, you cannot export arrays. So you have to serialize them before passing through the environment. This serialization useful other places too where only a string fits (su -c 'string', ssh host 'string'). The shortest code way to do this is to abuse 'getopt'
Use it like this:
This does not address unset/sparse arrays.
You might be able to reduce the 2 'eval' calls in restore_array.
尽管这个问题/答案已经很老了,但在搜索“bash序列化数组”时,这篇文章似乎是最热门的
,而且,尽管原始问题与不太相关对于序列化/反序列化数组,答案似乎确实已经朝这个方向发展。
因此...我提供我的解决方案:
优点
缺点
serialize_array.bash
中注意:这是作为要点托管在此处:
[编辑]
Although this question/answers are pretty old, this post seems to be the top hit when searching for
"bash serialize array"
And, although the original question wasn't quite related to serializing/deserializing arrays, it does seem that the answers have devolved in that direction.
So with that ... I offer my solution:
Pros
Cons
serialize_array.bash
NOTE: This is hosted as a gist here:
[edits]
你(嗨!)可以使用这个,不需要编写文件,对于 ubuntu 12.04,bash 4.2.24
另外,你的多行数组可以导出。
cat >>exportArray.sh
在终端 bash 上测试此示例(适用于 bash 4.2.24):
我可以改进它 此处
PS.:如果有人清除/改进/makeItRunFaster 我想知道/看到,谢谢! :D
you (hi!) can use this, dont need writing a file, for ubuntu 12.04, bash 4.2.24
Also, your multiple lines array can be exported.
cat >>exportArray.sh
test this example on terminal bash (works with bash 4.2.24):
I may improve it here
PS.: if someone clears/improve/makeItRunFaster I would like to know/see, thx! :D
对于值不带空格的数组,我一直在使用一组简单的函数来迭代每个数组元素并连接数组:
第一个函数通过添加左括号和右括号并转义所有双引号。第二个函数将去掉引号和括号并将它们放入虚拟数组中。
为了导出数组,您需要传入原始数组的所有元素:
此时,数组已导出到值 $arrayString 中。要导入目标文件中的数组,请重命名该数组并执行相反的转换:
For arrays with values without spaces, I've been using a simple set of functions to iterate through each array element and concatenate the array:
The first function with turn the array into a string by adding the opening and closing parentheses and escaping all of the double quotation marks. The second function will strip the quotation marks and the parentheses and place them into a dummy array.
In order export the array, you would pass in all the elements of the original array:
At this point, the array has been exported into the value $arrayString. To import the array in the destination file, rename the array and do the opposite conversion:
非常感谢@stéphane-chazelas,他指出了我之前尝试中的所有问题,现在这似乎可以将数组序列化到标准输出或变量中。
此技术不会对输入进行 shell 解析(与
declare -a
/declare -p
不同),因此可以安全地防止在序列化文本中恶意插入元字符。注意:换行符不会被转义,因为
read
会删除\
字符对,因此必须传递-d ...
读取,然后保留未转义的换行符。所有这些都在
unserialise
函数中进行管理。使用两个魔术字符,字段分隔符和记录分隔符(以便多个数组可以序列化到同一个流)。
这些字符可以定义为
FS
和RS
,但都不能定义为newline
字符,因为转义的换行符会被read< 删除/代码>。
转义字符必须是
\
反斜杠,因为read
使用它来避免字符被识别为IFS
字符。serialise
将把"$@"
序列化到 stdout,serialise_to
将序列化为$1
中命名的变量,并使用反序列化:
或
例如
(没有尾随换行符)
读回它:
或者
Bash 的
read
尊重转义字符\
(除非您传递 -r 标志)以删除字符的特殊含义,例如 for输入字段分隔或行定界。如果你想序列化一个数组而不是单纯的参数列表,那么只需将你的数组作为参数列表传递:
你可以在循环中使用
\n'}" Now is the time For all good men To drink $drink At the `party` Party Party Partyunserialise
,就像read
一样,因为它只是一个包装的读取 - 但请记住该流不是换行符分隔的:或者
Bash 的
read
尊重转义字符\
(除非您传递 -r 标志)以删除字符的特殊含义,例如 for输入字段分隔或行定界。如果你想序列化一个数组而不是单纯的参数列表,那么只需将你的数组作为参数列表传递:
你可以在循环中使用
Party\tParty\tParty' Now is the time;For all good men;To drink $drink;At the `party`;Party Party Party:unserialise
,就像read
一样,因为它只是一个包装的读取 - 但请记住该流不是换行符分隔的:(没有尾随换行符)
读回它:
或者
Bash 的
read
尊重转义字符\
(除非您传递 -r 标志)以删除字符的特殊含义,例如 for输入字段分隔或行定界。如果你想序列化一个数组而不是单纯的参数列表,那么只需将你的数组作为参数列表传递:
你可以在循环中使用
unserialise
,就像read
一样,因为它只是一个包装的读取 - 但请记住该流不是换行符分隔的:Much thanks to @stéphane-chazelas who pointed out all the problems with my previous attempts, this now seems to work to serialise an array to stdout or into a variable.
This technique does not shell-parse the input (unlike
declare -a
/declare -p
) and so is safe against malicious insertion of metacharacters in the serialised text.Note: newlines are not escaped, because
read
deletes the\<newlines>
character pair, so-d ...
must instead be passed to read, and then unescaped newlines are preserved.All this is managed in the
unserialise
function.Two magic characters are used, the field separator and the record separator (so that multiple arrays can be serialized to the same stream).
These characters can be defined as
FS
andRS
but neither can be defined asnewline
character because an escaped newline is deleted byread
.The escape character must be
\
the backslash, as that is what is used byread
to avoid the character being recognized as anIFS
character.serialise
will serialise"$@"
to stdout,serialise_to
will serialise to the varable named in$1
and unserialise with:
or
e.g.
(without a trailing newline)
read it back:
or
Bash's
read
respects the escape character\
(unless you pass the -r flag) to remove special meaning of characters such as for input field separation or line delimiting.If you want to serialise an array instead of a mere argument list then just pass your array as the argument list:
You can use
\n'}" Now is the time For all good men To drink $drink At the `party` Party Party Partyunserialise
in a loop like you wouldread
because it is just a wrapped read - but remember that the stream is not newline separated:or
Bash's
read
respects the escape character\
(unless you pass the -r flag) to remove special meaning of characters such as for input field separation or line delimiting.If you want to serialise an array instead of a mere argument list then just pass your array as the argument list:
You can use
Party\tParty\tParty' Now is the time;For all good men;To drink $drink;At the `party`;Party Party Party:unserialise
in a loop like you wouldread
because it is just a wrapped read - but remember that the stream is not newline separated:(without a trailing newline)
read it back:
or
Bash's
read
respects the escape character\
(unless you pass the -r flag) to remove special meaning of characters such as for input field separation or line delimiting.If you want to serialise an array instead of a mere argument list then just pass your array as the argument list:
You can use
unserialise
in a loop like you wouldread
because it is just a wrapped read - but remember that the stream is not newline separated:我为此编写了自己的函数,并使用
IFS
改进了该方法:功能:
$(...)
因此不会产生另一个 bash shell 进程?
和|
字符序列化为?00
和?01
序列和返回,因此可以在带有这些字符的数组上使用cygwin bash 3.2.48
和Linux bash 4.3.48
中测试代码>示例:
I've wrote my own functions for this and improved the method with the
IFS
:Features:
$(...)
and so doesn't spawn another bash shell process?
and|
characters into?00
and?01
sequences and back, so can be used over array with these characterscygwin bash 3.2.48
andLinux bash 4.3.48
Example:
我认为你可以尝试这种方式(通过在导出后采购你的脚本):
export myArray=(Hello World)
。你的脚本.sh
I think you can try it this way (by sourcing your script after export):
export myArray=(Hello World)
. yourScript.sh