在像下面这样的简单循环中
for f in $(ls -1) ;
do
something
done
Run Code Online (Sandbox Code Playgroud)
我想将输出的每一行存储ls -1在变量中f。
有没有办法在没有设置的情况下做到这一点IFS=$'\n'?
ter*_*don 14
正如我们在评论中所说,不要解析ls它很容易出错,而且完全没有必要。所有你需要的是
for f in *;
do
something
done
Run Code Online (Sandbox Code Playgroud)
这将遍历当前目录中的文件和目录1并将它们中的每一个(空格和所有)另存为$f. 例如:
$ ls -A1
file1
file 2
$ for f in *; do echo "File is '$f'"; done
File is 'file1'
File is 'file 2'
Run Code Online (Sandbox Code Playgroud)
1在 bash 中,这将忽略以.(隐藏文件)开头的文件/目录名称,除非您已设置dotglob为shopt -s dotglob.