Are wildcards ever supposed to be expanded for a program that is not the first in the pipeline? Every example uses wildcard for the first program in the pipelilne.

For example, the following does not work:

ls|grep *.h

And if I do it like this, then it is grep and not the shell that expands the wildcard(?)

ls|grep -E "*.h"

Is there an example where the wildcard is not the first pipe? The reason I'm asking is that I'm implementing a shell and can do wildcards for the first program in the pipeline e.g. ls *.h but I don't know if it should do it for subsequent pipes.

The background is that I'm writing my own shell and I've implemented wildcard:

$ ls *.c|grep mai

  • 2
    What are you trying to do in that grep example? grep's first argument should be a pattern. If there are no .h files present, it will search for *.h in standard input (the output of ls). If there is only one .h file present, it will try to find that filename in the ls output; and if there are more, it will look for the first filename inside each of the rest, ignoring the ls output.
    – JigglyNaga
    May 12, 2016 at 15:46
  • I'm writing a shell and implementing wildcard expansion. It has to do it for each pipe in a pipeline then. You may check out my shell project. I think that maybe it is rare that wildcards are at the end, but it should work. May 12, 2016 at 16:49

1 Answer 1


One example:

$ echo jeff > jeff
$ echo not > not
$ echo 's/jeff/not/' | sed -i -f - *
$ cat jeff

For more information on what standard shells should do with filename/pathname expansion (globbing), see:

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_06 where it says:

After field splitting, if set -f is not in effect, each field in the resulting command line shall be expanded using the algorithm described in Pattern Matching Notation, qualified by the rules in Patterns Used for Filename Expansion.

  • 1
    Now my shell can do wildcards at the end of expressions. I just tested your pipeline and it worked. Thanks a lot for the information. It's always interesting to learn about Unix. My goal is to write a POSIX-compliant shell but I don't know what that is yet. May 13, 2016 at 2:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .