Bash循环处理带有空格的文件名

作者:crane-yuan 日期:2017-05-02


解决方法

使用IFS(the Internal Field Separator),Shell依靠它去决定如何进行单词分隔。

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

for f in *
do
  echo "$f"
done

IFS=$SAVEIFS

建议

在Linux或Unix系统中,命名文件时,最好不要带有空格,这会给自在带来麻烦的。

参考文章

  • BASH Shell: For Loop File Names With Spaces
  • SHELL技巧:处理文件名中的那些空格
  • Work the Shell - Dealing with Spaces in Filenames

你可能感兴趣的:(Bash循环处理带有空格的文件名)