调用 find 的 bash 脚本中的语法错误

use*_*992 1 bash find quoting

请问这个脚本的错误在哪里:

#!/bin/bash
rep="git"

files=`find' ${rep} '-type f`
for f in ${files} do
    echo $f
done
Run Code Online (Sandbox Code Playgroud)

当我find git -type f在 shell 中单独运行时,它起作用了!

cho*_*oba 6

单引号中的字符串没有内插。这意味着,您正在尝试运行

find ' ${rep} '-type f
Run Code Online (Sandbox Code Playgroud)

去掉单引号。如果您确实需要引用 $rep(例如因为它包含空格),请使用双引号:

files=`find "$rep" -type f`
Run Code Online (Sandbox Code Playgroud)

请注意,双引号内没有空格。您正在搜索“git”,而不是“git”,对吗?