This question already has answers here:
When to wrap quotes around a shell variable?
(5个答案)
File names with spaces in BASH
(7个答案)
去年关门了。
我试图通过一个显示文件大小的目录运行,但当我试图打印一个有spaces stat命令的文件大小时失败。我该怎么解决?
#!/bin/bash

    for file in /home/user/Desktop/*; do
        fileSize=$(stat -c%s $file)
        echo $fileSize
    done

最佳答案

使用引号。

#!/bin/bash
for file in /home/user/Desktop/*; do
    fileSize=$(stat -c%s "$file")
    echo $fileSize
done

为我的桌面调整-
$: for file in *
   do case "$f" in
      *\ *) printf "'$f': %d\n" $(stat -c%s "$file")
      esac
   done
'BPS Stuff': 0
'New TWC Account -  Hodges  Paul A.msg': 37888
'Nov FTOTD calendar.JPG': 138769
'OCA Web Client - ASAP.lnk': 2406
'Paul - Copy.png': 64915
'Solstice Client.lnk': 2165
'VIP Access.lnk': 2079

关于linux - 显示名称包含空格的文件大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53216767/

10-10 04:59