简体   繁体   中英

printing folder names in bash

This piece of bash code, shows no folder name while there exists many folders.

#!/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. use something like this instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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