Linux 在bash中打印文件夹名称

Linux 在bash中打印文件夹名称,linux,bash,Linux,Bash,这段bash代码没有显示文件夹名称,但存在许多文件夹 #!/bin/bash for file in .; do if [ -d $file ]; then echo $file fi done 输出仅为 您能解释一下原因吗?它将读取为大小为1的数组,然后为您打印。改用类似的方式: for file in `ls`; do if [ -d $file ]; then echo $file fi done 它将读取为大小为1的数组,并为您打印。改用类似

这段bash代码没有显示文件夹名称,但存在许多文件夹

#!/bin/bash

for file in .; do
  if [  -d $file ]; then 
    echo $file
  fi
done
输出仅为

您能解释一下原因吗?

它将
读取为大小为1的数组,然后为您打印。改用类似的方式:

for file in `ls`; do
  if [  -d $file ]; then 
    echo $file
  fi
done

它将
读取为大小为1的数组,并为您打印。改用类似的方式:

for file in `ls`; do
  if [  -d $file ]; then 
    echo $file
  fi
done
,改用glob(即,
用于*中的文件;do
),改用glob(即,
用于*中的文件;do