linux – 来自bash one liner的奇怪输出
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux – 来自bash one liner的奇怪输出,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1629字,纯文字阅读大概需要3分钟。
内容图文
![linux – 来自bash one liner的奇怪输出](/upload/InfoBanner/zyjiaocheng/955/682eb7671ad54c3785c2ee6f418162fc.jpg)
在学习bash的过程中,我经常调整现有的东西并看到它的输出.
~$for i in {1..19}; do echo "Everything in UNIX is a file."; sleep 1; done
我有这个,出于好奇,我将上面的内容调整为以下内容: –
~$for i in {1..19 * 2}; do echo "Everything in UNIX is a file."; echo "The value of i is ${i}"; sleep 1; done
现在让我惊讶的是我开始得到以下输出: –
Everything in UNIX is a file.
The value of i is OneDrive
Everything in UNIX is a file.
The value of i is opera autoupdate
Everything in UNIX is a file.
The value of i is Personal_Workspace
Everything in UNIX is a file.
The value of i is Pictures
Everything in UNIX is a file.
The value of i is PrintHood
Everything in UNIX is a file.
The value of i is Recent
Everything in UNIX is a file.
The value of i is Roaming
Everything in UNIX is a file.
The value of i is Saved Games
Everything in UNIX is a file.
The value of i is Searches
i的一些值是我的主目录中的文件和目录的名称,我在主目录中,同时执行此脚本.
我期待i值的范围从1到19 * 2 = 38,所以我会从1,2,3 …… 30 …… 38取值.
但显然它不是为什么?
解决方法:
是的,在bash中,范围扩展发生在其他一切之前.由于扩展bash shell的顺序,你期望算术扩展能够按预期发生.您的代码最终将{1..19,*和2}解释为文字字符串.
因为*在shell中有特殊含义,它是一个glob扩展,列出了当前文件夹中的所有文件/目录.你也可以看到一个条目,说明字面上解释的其他两个字符串.
从扩展部分下的man bash(1)页面开始
The order of expansions is: brace expansion, tilde expansion, parameter, variable and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and pathname expansion.
如果你再次为bourne定位脚本shell,那么最好使用带((..))构造的for循环
for ((i=1; i<=38; i++)); do
内容总结
以上是互联网集市为您收集整理的linux – 来自bash one liner的奇怪输出全部内容,希望文章能够帮你解决linux – 来自bash one liner的奇怪输出所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。