Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:30:49+00:00 2026-06-09T20:30:49+00:00

I’m fairly new to bash scripts, so this is probably a stupid syntax error,

  • 0

I’m fairly new to bash scripts, so this is probably a stupid syntax error, but why is this code not working?

for x in $(ls)
do
  if [ -d $x ]
  then
  echo $x
  fi
done

The separate for and if section work fine on their own, but this produces no output.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:30:50+00:00Added an answer on June 9, 2026 at 8:30 pm

    Two things. Never use ls to iterate files, and quote parameter expansions "$x". The for and if syntax itself is correct. I prefer to put the do and then on the same line though

    for file in *; do
        if [[ -d "$file" ]]; then
            echo "$file is a directory"
        elif [[ -f "$file" ]]; then
            echo "$file is a regular file"
        fi
    done
    

    For learning bash, I recommend reading http://mywiki.wooledge.org/BashGuide most other tutorials and guides are unfortunately not very good.

    The reason for not doing for x in $(ls) to iterate files is because for iterates words and ls outputs lines with filenames. if those filenames happen to contain whitespace, those filenames will be split up further into words, so you’ll be iterating the words of the filenames, not the filenames. Obviously, for the simple cases that works, but why use a half-working solution when there’s a shorter and more elegant way that handles all cases?

    With for x in * the shell replaces the * with all filenames matching that pattern in the current directory (called pathname expansion), and each filename will be a separate word so it will work no matter what characters the filename contains. Filenames can contain any character (including newlines), except / and the NUL byte (\0).

    See http://mywiki.wooledge.org/ParsingLs for more on that.

    As for using [[ vs [. [ is a command inherited from the bourne shell, used to test strings, files and numbers. Bash has added a more powerful [[ keyword that can do everything [ can and more. If you’re writing an sh script, you must use [, but in bash scripts you should use the more powerful [[ and (( syntaxes. See http://mywiki.wooledge.org/BashFAQ/031 for more about the difference between [ and [[.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.