提问人:willco 提问时间:2/19/2022 最后编辑:Cyruswillco 更新时间:2/19/2022 访问量:880
在 for 循环中使用目录路径变量
Using a directory path variable in a for loop
问:
我有以下循环:for
for f in /home/Some\ path\ that\ includes\ spaces/* ; do
echo "$f"
done
当我运行这个循环时,它会吐出里面的所有内容,正如我所期望的那样。但是,如果我尝试将文件路径定义为变量,如下所示:/home/Some path that includes spaces/
dir="/home/Some\ path\ that\ includes\ spaces/*"
for f in "$dir" ; do
echo "$f"
done
输出只是文件路径本身。我确定我缺少一些简单的语法,但我无法在网上找到有关它的任何信息。
答:
1赞
choroba
2/19/2022
#1
通过包含引号,可以防止其扩展。*
dir="/home/Some path that includes spaces"
for f in "$dir"/* ; do
echo "$f"
done
评论
0赞
John Kugelman
2/19/2022
您能解释一下为什么将球体移出会起作用吗?
0赞
willco
2/19/2022
这解决了,非常感谢!
评论