linux – Bash for循环数组,文件来自(*)只显示第一个元素
发布时间:2020-12-15 07:33:39 所属栏目:Linux 来源:互联网
导读:我想将当前目录的文件放在一个数组中,并用这个脚本回显每个文件: #!/bin/bashfiles=(*)for file in $filesdo echo $filedone# This demonstrates that the array in fact has the values from (*)echo ${files[0]} ${files[1]} ec
我想将当前目录的文件放在一个数组中,并用这个脚本回显每个文件: #!/bin/bash files=(*) for file in $files do echo $file done # This demonstrates that the array in fact has the values from (*) echo ${files[0]} ${files[1]} echo done 输出: echo.sh echo.sh read_output.sh done 有谁知道为什么只有第一个元素在for循环中打印? 解决方法$files扩展到数组的第一个元素.尝试echo $files,它只会打印数组的第一个元素. 由于同样的原因,for循环只打印一个元素. 要扩展到数组的所有元素,您需要将其写为${files [@]}. 迭代Bash数组元素的正确方法: for file in "${files[@]}" (编辑:4S站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- Install Tomcat 6 on CentOS or RHEL --转载
- linux – 检测stdout是否被重定向到管道(而不是文
- linux-kernel – dirty_expire_centisecs的实现
- linux – 为什么glibc二进制文件名为libc.so.6而
- linux – Ubuntu / Mint上的PhpStorm更新
- 如何在Linux上从PHP创建与WinZip兼容的AES-256加
- /usr/local/bin/python3:bad interpreter:ubun
- 期望在Linux中,fd 打开文件描述符的最大数量是否
- 如何使用cURL与PHP同时打开多个URL?
- 如何确保我的Linux程序不产生核心转储?
热点阅读