mahmood
mahmood

Reputation: 24665

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?

Upvotes: 1

Views: 90

Answers (1)

none
none

Reputation: 12087

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

Upvotes: 5

Related Questions