zsh 像在 bash 中一样进行迭代 [英] zsh to iterate like in bash

查看:39
本文介绍了zsh 像在 bash 中一样进行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从 bash 切换到 zsh,但不确定如何实现:

I recently switched from bash to zsh and not sure how to achieve this:

重击:

$ list='aaa
> bbb
> ccc
> ddd'

$ for i in $list; do echo $i-xxx; done
aaa-xxx
bbb-xxx
ccc-xxx
ddd-xxx

在 zsh 中我得到:

in zsh I get:

% list='aaa
quote> bbb
quote> ccc
quote> ddd'

% for i in $list; do echo $i-xxx; done
aaa
bbb
ccc
ddd-xxx

如何强制 zsh 以与 bash 相同的方式执行此操作?

how to force zsh to do this in the same way as bash?

推荐答案

您可以使用参数扩展标志在换行符处拆分字符串:

You can use a parameter expansion flag to split the string at newlines:

for line in ${(f)list}; do ...

参见 http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags

这篇关于zsh 像在 bash 中一样进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆