分配非数组变量时的文件名扩展
filename expansion on assigning a non-array variable
这是关于 Zsh 5.5.1 的。
假设我有一个扩展到一个文件的 glob 模式,我想将这个文件分配给一个变量。这有效:
# N: No error if no files match. D: Match dot files. Y1: Expand to exactly one entry.
myfile=(*(NDY1))
和 echo $myfile
将显示文件(或目录)。但是这个不行:
myfile=*(NDY1)
在后一种情况下,echo $myfile
保持模式,即 *(NDY1)
。
当然我可以做一些便宜的把戏,比如通过
创建一个chilprocess
myfile=$(echo *(NDY1))
但是有没有办法不用这些技巧就可以进行assinment?
默认情况下,zsh 不会在标量分配中进行文件名扩展,但选项 GLOB_ASSIGN 可以提供帮助。 (提供此选项只是为了向后兼容。)
local myfile=''
() {
setopt localoptions globassign
myfile=*(NDY1)
}
echo $myfile
;#>> something
以下是 zsh 文档中的一些描述:
The value of a scalar parameter may also be assigned by writing:
name=value
In scalar assignment, value is expanded as a single string, in which the elements of arrays are joined together; filename expansion is not performed unless the option GLOB_ASSIGN is set.
GLOB_ASSIGN
<C>
If this option is set, filename generation (globbing) is performed on the right hand side of scalar parameter assignments of the form 'name=pattern
(e.g. foo=*
'). If the result has more than one word the parameter will become an array with those words as arguments. This option is provided for backwards compatibility only: globbing is always performed on the right hand side of array assignments of the form name=(value)
(e.g. foo=(*)
) and this form is recommended for clarity; with this option set, it is not possible to predict whether the result will be an array or a scalar.
--- zshoptions(1), GLOB_ASSIGN
, Expansion and Globbing, Description Of Options, zsh options
这是关于 Zsh 5.5.1 的。
假设我有一个扩展到一个文件的 glob 模式,我想将这个文件分配给一个变量。这有效:
# N: No error if no files match. D: Match dot files. Y1: Expand to exactly one entry.
myfile=(*(NDY1))
和 echo $myfile
将显示文件(或目录)。但是这个不行:
myfile=*(NDY1)
在后一种情况下,echo $myfile
保持模式,即 *(NDY1)
。
当然我可以做一些便宜的把戏,比如通过
创建一个chilprocessmyfile=$(echo *(NDY1))
但是有没有办法不用这些技巧就可以进行assinment?
默认情况下,zsh 不会在标量分配中进行文件名扩展,但选项 GLOB_ASSIGN 可以提供帮助。 (提供此选项只是为了向后兼容。)
local myfile=''
() {
setopt localoptions globassign
myfile=*(NDY1)
}
echo $myfile
;#>> something
以下是 zsh 文档中的一些描述:
The value of a scalar parameter may also be assigned by writing:
name=value
In scalar assignment, value is expanded as a single string, in which the elements of arrays are joined together; filename expansion is not performed unless the option GLOB_ASSIGN is set.
GLOB_ASSIGN
<C>
If this option is set, filename generation (globbing) is performed on the right hand side of scalar parameter assignments of the form 'name=pattern
(e.g.foo=*
'). If the result has more than one word the parameter will become an array with those words as arguments. This option is provided for backwards compatibility only: globbing is always performed on the right hand side of array assignments of the formname=(value)
(e.g.foo=(*)
) and this form is recommended for clarity; with this option set, it is not possible to predict whether the result will be an array or a scalar.--- zshoptions(1),
GLOB_ASSIGN
, Expansion and Globbing, Description Of Options, zsh options