linux – Bash for循环数组,文件来自(*)只显示第一个元素
发布时间:2020-12-15 06:47:49 所属栏目: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[@]}" (编辑:岳阳站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- linux – dhclient:“RTNETLINK回答:文件存在”是什么意思
- Another app is currently holding the yum lock
- 如何使用Linux在Linux机器上获取文件夹的所有者和组?
- linux – 为什么iptables拒绝允许数据包的第二个和后续片段
- 如何在linux中控制鼠标移动?
- linux – 用于安装AWS CLI工具的Bash脚本
- linux – 如何比较两个目录的大小?
- linux-kernel – 有人可以帮我替换块设备驱动程序上的“loc
- linux – tcpdump中“未知SSAP”和“未知DSAP”是什么意思?
- LINUX入门:Linux Shell脚本中read、重定向和文件句柄和进程