这个问题的灵感来自
我看到这些结构
for file in `find . -type f -name ...`; do smth with ${file}; done
Run Code Online (Sandbox Code Playgroud)
和
for dir in $(find . -type d -name ...); do smth with ${dir}; done
Run Code Online (Sandbox Code Playgroud)
几乎每天都在这里使用,即使有些人花时间对这些帖子发表评论,解释为什么应该避免这种东西......
看到此类帖子的数量(以及有时这些评论被简单地忽略的事实)我想我还不如问一个问题:
为什么循环find的输出是不好的做法,为 返回的每个文件名/路径运行一个或多个命令的正确方法是find什么?
过去的旧建议是对任何涉及 a 的表达式加双引号$VARIABLE,至少在希望 shell 将其解释为单个项目的情况下,否则,内容中的任何空格$VARIABLE都会脱离 shell。
但是,我知道在较新版本的 shell 中,不再总是需要双引号(至少出于上述目的)。例如,在bash:
% FOO='bar baz'
% [ $FOO = 'bar baz' ] && echo OK
bash: [: too many arguments
% [[ $FOO = 'bar baz' ]] && echo OK
OK
% touch 'bar baz'
% ls $FOO
ls: cannot access bar: No such file or directory
ls: cannot access baz: No such file or directory
Run Code Online (Sandbox Code Playgroud)
在zsh,而另一方面,同样的三个命令成功。因此,基于此实验,似乎在 中bash可以省略 内部的双引号[[ ... ]],但不能省略内部 …
我显然明白可以为内部字段分隔符变量添加值。例如:
$ IFS=blah
$ echo "$IFS"
blah
$
Run Code Online (Sandbox Code Playgroud)
我也明白这read -r line会将数据保存stdin到名为的变量line:
$ read -r line <<< blah
$ echo "$line"
blah
$
Run Code Online (Sandbox Code Playgroud)
但是,命令如何分配变量值?它是否首先存储来自stdinto 变量的数据line,然后赋予lineto 的值IFS?
为什么我$x从下面的片段中得到不同的值?
#!/bin/bash
x=1
echo fred > junk ; while read var ; do x=55 ; done < junk
echo x=$x
# x=55 .. I'd expect this result
x=1
cat junk | while read var ; do x=55 ; done
echo x=$x
# x=1 .. but why?
x=1
echo fred | while read var ; do x=55 ; done
echo x=$x
# x=1 .. but why?
Run Code Online (Sandbox Code Playgroud)