对我来说,它的工作:
diretorio=$(echo 'test 123'*)
但不是当我用引号
Var2="test 123"
diretorio=$(echo '$Var2'*)
变量如何解决它的工作?
对我来说,它的工作:
diretorio=$(echo 'test 123'*)
但不是当我用引号
Var2="test 123"
diretorio=$(echo '$Var2'*)
变量如何解决它的工作?
我可以建议替代方案吗? ACH?而不是使文件名的空格分隔的列表(这将导致可怕的混乱,如果任何文件名包含空格,如“测试123”),使用数组:
diretorio=("${Var2}"*)
doSomethingWithAllFiles "${diretorio[@]}"
for umDiretorio in "${diretorio[@]}"; do
doSomethingWithASingleFile "$umDiretorio"
done
这个错误在你glob是
diretorio=$(echo '$Var2'*)
是/dev/null
了一枪,因为外壳不扩大在单引号中的变量。
所以:
diretorio=$(echo "$Var2"*)
学习“和”和'之间的区别见http://mywiki.wooledge.org/Quotes和http://wiki.bash-hackers.org/syntax/words
使用双引号:
diretorio=$(echo "$Var2"*)
单身者防止变量替换
已经说过;) – 2013-03-14 22:54:37
@sputnick确实,虽然在我写回复时还没有传播到我的浏览器。 – jlliagre 2013-03-14 23:01:56
对不起 - 这是我不清楚你想要实现。 – suspectus 2013-03-14 22:51:23
为什么在第一个地方你将一个连接的文件名列表放入一个变量中? – ormaaj 2013-03-15 00:00:23
另一个有趣的混乱:'a =“*”; echo $ a;一个= “ '*'”; echo $ a' - 没有简单的方法来禁用通配符扩展而不会引入其他问题,请考虑'if [-n“$ 1”];那么a =“ - name $ 1”; fi; find/dir $ a'(Arrays也适用于这种情况......) – 2016-06-17 07:03:45