Search code examples
command-linewildcardzshglob

How exactly does zsh expand globs?


I have a C program that displays it's command-line by iterating through the argv variable.

#include <stdio.h>

int main(int argc, char *argv[]){
    int i = 0;
    printf("----------\n");
    for(i = 0; i < argc; i++)
        printf("%s\n", argv[i]);
    return 0;
}

I invoked the program in a folder containing a large C++ source tree like this:

./a.out **/*.h

The output:

zsh: argument list too long: ./a.out

However, programs like ls and grep work without any problems when invoked using the **/*.h glob in the same folder. Why does zsh fail when invoking my program? How does zsh go about expanding wildcards?

Edit: I'm using zsh on cygwin.


Solution

  • Is the application you tried to run a Windows app (including mingw) or a Cygwin app?

    ARG_MAX defines how long the command line can be. Usually it's set by the OS, and all applications are limited to it, but on Cygwin applications compiled for Cygwin can use a larger buffer than Windows apps - see this message for an example discussion.

    If you don't necessarily require all the files as args at the same time, you can use xargs to partition the filenames to blocks that fit in ARG_MAX:

    echo **/*.h | xargs ./a.out