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 8696675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:19:48+00:00 2026-06-13T01:19:48+00:00

set +f; rm *; touch a; for i in *; do touch b; echo

  • 0
set +f; rm *; touch a; for i in *; do touch b; echo $i; done

In all the shells I tried (dash, ksh, zsh, bash), the above snippet outputs only “a”. Implementing the same in C, (opendir/loop on readdir creating a file) also outputs only “a”. However, if the directory contains enough files (~4096), the C implementation usually outputs “b” as well. (ie, readdir returns a result for the file created after opendir). I see nothing in the shell standard that indicates how the shell ought to respond in this case. Could a standard compliant shell enter the loop for the file created after the glob? This would be a highly desirable trait, since it would mean the shell is not reading the entire glob into memory before performing any actions. In situations where directories are expected to contain many files, it often takes several seconds just to read the glob into memory, which is time wasted.

Is there any shell implementation that does not read the entire glob into memory before entering the loop?

  • 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-13T01:19:49+00:00Added an answer on June 13, 2026 at 1:19 am

    No. The context into which the glob is expanding is essentially identical to an ordinary command expansion context wherein all expansions are processed and the resulting words saved in an immutable fashion for iteration. There is no lazy iterator possible with a for-in loop. Of course, expansions can be side-effectful and mixed with globs, so they have to be eagerly evaluated. This is why find -exec [+;] is still so frequently recommended over globstar when it’s possible to do things concurrently.

    I can’t really say anything about this 4096 issue. I don’t think the two are really comparable. Shell for..in just expands the words and iterates them.

    A related FAQ is whether you can do things like read ahead to the next values to be assigned. As far as I’m aware there are no bourne-like shells that give any extra access to the word list. You’d have to use arrays for that. Basically all the limitations of for..in can be overcome via arrays.

    Here is a funny lazy coproc generator I wrote for Bash. It’s pretty useless.

    coproc x { while :; do find . -type f -maxdepth 1 -exec sh -c 'read; echo "$1"' -- {} \;; done; };
    
    while :; do
        echo 1 >&"${x[1]}"
        read -ru "${x[0]}" file
        echo "$file"
        sleep 1
    done
    

    And one more for..in tidbit that doesn’t really have to do with the question — In ksh93, and Bash’s git devel branch, it is possible to make use of the “control variable” in an interesting way.

    function f {
        nameref x # Chet may decide not to emulate the typeset -n aliases
    
        for x; do
            x=hi
        done
    }
    
    typeset -a arr
    f 'arr['{0..3}']'
    typeset -p arr # arr=(hi hi hi hi)
    

    Each iteration assigns a reference to the given object to x. Of course in ksh that can be any arbitrary complex datatype. I suppose this might be abused to somehow simulate lazyness. Unfortunately this pattern doesn’t seem to work in mksh.

    Edit Forgot since writing this that I found a lot of shells do in fact optimize for x syntax. I assume at least for x in is copy-on-write and only makes a copy of the positional parameters if shift or set is used within the loop.

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

Sidebar

Related Questions

in sencha touch 2pr4, is it possible to set some property for input (xtype:passwordfield)
I have a Sencha Touch 2.0 app that is set up with a card
I'm currently trying to set up a web page for a touch screen computer,
I have a script test.sh #/!bin/bash set -e trap errorhandler ERR errorhandler(){ status=$? trap
Currently trying to set up and deploy a Sencha Touch 2 application. I'm following
In sencha touch 2, it appears there are only string, int, float, boolean data
I've set scrollView.userInteractionEnabled to NO so that superview catches the touch event. In appropriate
I have a spark datagrid on a mobile application, I set the interactionMode=touch and
I have a code which has set of buttons listening for touch events. for
In my Cocoa Touch static library project, I have a target set apart for

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.