在 bash 中read
,内置命令可以-d
让我们指定换行符以外的行分隔符
是否readarray
提供某种方式来指定行分隔符?
- 它没有用于此目的的选项,这是正确的吗?
- 是否有用于此目的的 shell 默认变量,类似于
IFS
字段分隔符?
谢谢。
看到steeldriver的评论后,
$ bash --version
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ help readarray
readarray: readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
Read lines from a file into an array variable.
A synonym for `mapfile'.
但后来我发现:
$ help mapfile
mapfile: mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
Read lines from the standard input into an indexed array variable.
Read lines from the standard input into the indexed array variable ARRAY, or
from file descriptor FD if the -u option is supplied. The variable MAPFILE
is the default ARRAY.
Options:
-d delim Use DELIM to terminate lines, instead of newline
...
答案1
从 bash 4.4 版开始,是的。从发布公告来看Bash-4.4 版本可用:
最值得注意的新功能是 Mapfile 使用任意记录分隔符的能力;
(readarray
是 的同义词mapfile
)。中的描述man bash
是
-d The first character of delim is used to terminate each
input line, rather than newline.