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

关于linux - 在 bash 中打印文件夹名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13094165/

相关文章:

linux - 如何让atop排除开机后的统计数据?

c - Lib(s)uinput : creating joystick with more than one button

linux - 如何从列表中重新附加和关闭多个 screen session

bash - 将一个文件的标题插入到另一个文件的顶部

linux - 通过 CGI 执行 bash 脚本

linux - Docker 中的 Usbip

linux - 一天中特定时间段之间的调用旋度

bash - ffmpeg - 如何并行转换大量文件?

linux - 每当运行不同的程序时,如何使 bash 脚本运行?

linux - bash 字符串比较没有按预期工作 - 如何修复