包含空格的文件名shell脚本问题 [英] Shell script issue with filenames containing spaces
问题描述
我明白,用空格在文件名中处理的一种技术是用单引号括文件名:'
I understand that one technique for dealing with spaces in filenames is to enclose the file name with single quotes: "'".
为什么是它下面的code称为echo.sh适用于含有空格的文件名的目录,但该方案ls.sh不工作,在这里唯一的区别是回响所取代以'ls'的?
Why is it that the following code called, "echo.sh" works on a directory containing filenames with spaces, but the program "ls.sh" does Not work, where the only difference is 'echo' replaced with 'ls'?
echo.sh
#!/bin/sh
for f in *
do
echo "'$f'"
done
生成:结果
A B C'结果
D E F'结果
echo.sh结果
ls.sh'结果
Produces:
'a b c'
'd e f'
'echo.sh'
'ls.sh'
但是,ls.sh失败:
But, "ls.sh" fails:
#!/bin/sh
for f in *
do
ls "'$f'"
done
生成:结果
LS:无法访问A B C:没有这样的文件或目录结果
LS:无法访问D E F':没有这样的文件或目录结果
LS:无法访问'echo.sh':没有这样的文件或目录结果
LS:无法访问'ls.sh:没有这样的文件或目录搜索
Produces:
ls: cannot access 'a b c': No such file or directory
ls: cannot access 'd e f': No such file or directory
ls: cannot access 'echo.sh': No such file or directory
ls: cannot access 'ls.sh': No such file or directory
推荐答案
你实际上增加冗余'(这你回声调用显示)
you're actually adding redundant "'" (which your echo invocation shows)
试试这个:
#!/bin/sh
for f in *
do
ls "$f"
done
这篇关于包含空格的文件名shell脚本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!