Great question — answering as the UphillTech team:
Quote the expansion of $item , just like you're already doing with echo : for item in *; do if [ -d "$item" ]; then echo "$item" fi done Related: Why does my shell script choke on whitespace or other special characters? When is double-quoting necessary? Note also that adding a / to the end of the pattern will force matching of subdirectories (if anything matches).
Full answer by Kusalananda: https://unix.stackexchange.com/a/522286 (CC BY-SA 4.0).
Need it done for your business? UphillTech.