Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在bash中将空间视为换行符_Bash - Fatal编程技术网

在bash中将空间视为换行符

在bash中将空间视为换行符,bash,Bash,我编写了一个bash,只是为了显示给定目录中所有文件的名称,但是当我运行它时,它会破坏包含空格的文件名 if [ $# -eq 0 ] then echo "give a source directory in the command line argument in order to rename the jpg file" exit 1 fi if [ ! -d "$1" ]; then exit 2 fi if [ -d "$1" ] then for i in $(ls "$1")

我编写了一个bash,只是为了显示给定目录中所有文件的名称,但是当我运行它时,它会破坏包含空格的文件名

if [ $# -eq 0 ]
then
echo "give a source directory in the command line argument in order to rename the jpg file"
exit 1
fi

if [ ! -d "$1" ]; then
exit 2
fi

if [ -d "$1" ]
then
for i in $(ls "$1")
do
echo "$i"
done
fi
当我运行bash脚本时,我得到了以下结果

21151991jatinkhurana_image
(co
py).jpg
24041991jatinkhurana_im
age.jpg
35041991jatinkhurana_image
.jpg
到目前为止,我一直在尝试重置IFS变量,如IFS=$echo-en\t\n\0,但没有发现任何更改


如果有人知道,请帮助我……

不要循环ls的结果。解析ls会使世界变得更糟,请阅读:

相反,您可以使用*扩展到给定目录中的现有内容:

for file in /your/dir/*
do
   echo "this is my file: $file"
done
使用变量:

for file in $dir/*
do
   echo "this is my file: $file"
done

我不会将此作为回复,因为我确信这是一个重复的问题。文件名中的空格在$ls$1中作为i的项目分隔符,只需在$ls$1中为i加引号。注:ls的无用用法,用法:for i,单位为1美元/*