简体   繁体   English

用bash打印文件夹名称

[英]printing folder names in bash

This piece of bash code, shows no folder name while there exists many folders. 这段bash代码在存在许多文件夹的情况下不显示文件夹名称。

#!/bin/bash

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

the output is only . 输出仅为. Can you explain why? 你能解释为什么吗?

it reads . 它读. as an array of size one and prints it for you. 作为大小为1的数组并为您打印。 use something like this instead: 使用类似这样的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM