bash 如何在zsh中一次遍历一个单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23157613/
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
How to iterate through string one word at a time in zsh
提问by Rob Bednark
How do I modify the following code so that when run in zsh it expands $things
and iterates through them one at a time?
如何修改以下代码,以便在 zsh 中运行时,它一次扩展$things
并遍历它们?
things="one two"
for one_thing in $things; do
echo $one_thing
done
I want the output to be:
我希望输出是:
one
two
But as written above, it outputs:
但如上所述,它输出:
one two
(I'm looking for the behavior that you get when running the above code in bash)
(我正在寻找在 bash 中运行上述代码时获得的行为)
回答by devnull
In order to see the behavior compatible with Bourne shell, you'd need to set the option SH_WORD_SPLIT
:
为了查看与 Bourne shell 兼容的行为,您需要设置选项SH_WORD_SPLIT
:
setopt shwordsplit # this can be unset by saying: unsetopt shwordsplit
things="one two"
for one_thing in $things; do
echo $one_thing
done
would produce:
会产生:
one
two
However, it's recommended to use an array for producing word splitting, e.g.,
但是,建议使用数组进行分词,例如,
things=(one two)
for one_thing in $things; do
echo $one_thing
done
You may also want to refer to:
您可能还想参考:
3.1: Why does $var where var="foo bar" not do what I expect?
回答by Kevin
You can use the z
variable expansion flag to do word splitting on a variable
您可以使用z
变量扩展标志对变量进行分词
things="one two"
for one_thing in ${(z)things}; do
echo $one_thing
done
Read more about this and other variable flags in man zshexpn
, under "Parameter Expansion Flags."
man zshexpn
在“参数扩展标志”下阅读有关此和其他变量标志的更多信息。
回答by Atttacat
You can assume the Internal Field Separator (IFS) on bash to be \x20 (space). This makes the following work:
您可以假设 bash 上的内部字段分隔符 (IFS) 为 \x20(空格)。这使得以下工作:
#IFS=$'\x20'
#things=(one two) #array
things="one two" #string version
for thing in ${things[@]}
do
echo $thing
done
With this in mind you can implement this in many different ways just manipulating the IFS; even on multi-line strings.
考虑到这一点,您可以通过多种不同的方式实现这一点,只需操纵 IFS;即使在多行字符串上。
回答by bgdnlp
Another way, which is also portable between Bourne shells (sh, bash, zsh, etc.):
另一种方式,也可以在 Bourne shell(sh、bash、zsh 等)之间移植:
things="one two"
for one_thing in $(echo $things); do
echo $one_thing
done
Or, if you don't need $things
defined as a variable:
或者,如果您不需要$things
定义为变量:
for one_thing in one two; do
echo $one_thing
done
Using for x in y z
will instruct the shell to loop through a list of words, y, z
.
Usingfor x in y z
将指示 shell 循环遍历单词列表,y, z
。
The first example uses command substitutionto transform the string "one two"
into a list of words, one two
(no quotes).
第一个示例使用命令替换将字符串"one two"
转换为单词列表one two
(无引号)。
The second example is the same thing without echo
.
第二个例子是同样的事情,没有echo
.
Here's an example that doesn't work, to understand it better:
这是一个不起作用的示例,以更好地理解它:
for one_thing in "one two"; do
echo $one_thing
done
Notice the quotes. This will simply print
注意引号。这将简单地打印
one two
because the quotes mean the list has a single item, one two
.
因为引号意味着列表只有一个项目,one two
.