Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question is a sequel of how does grep work?

It seems that the shell does the expansion of *.* to all applicable/available files and provide that to utilities like grep.

Why does the shell need to do that? Why not leave pathname expansion to the utility itself?

share|improve this question

migrated from Dec 1 '11 at 0:52

This question came from our site for professional and enthusiast programmers.

Why do hammer manufacturers screw the head to the handle? Why not leave that to the craftsman? – delnan Aug 21 '11 at 16:57
This is especially fun if you have a file named -rf and you use rm... – Kerrek SB Aug 21 '11 at 16:58
One thing that you should be aware of if you're coming from a Windows environment: the correct wildcard for "all non-hidden files in the current directory" is *, not *.*. It's very common in Unix/Linux to have files with no dots in their names. The system makes far less use of file extensions to decide what kind of file something is. For example, you'll commonly see "README" rather than "README.TXT". – Keith Thompson Aug 21 '11 at 18:05
and in Windows the shell does not do the expansion the utility does. So allowing rename *.txt *.bak – Mark Aug 26 '11 at 12:38
@Mark: Thanks. Windows is really a beautiful piece of code :D – hari Aug 26 '11 at 19:13
up vote 4 down vote accepted

Well one reason is that every utility would have to implement that functionality,. If the shell does it, it's only implemented once.

share|improve this answer

Because if the shell didn't provided this feature, every single small utility would have to implement it.

This also ensures that the expansion works exactly the same way for every command.

BTW the expansion does not occur only in command arguments, it can occur anywhere in a shell script; for example:

for file in *.c; do something; done
share|improve this answer
Not quite anywhere, though: in case $foo in *) command;; esac the * is syntactically a pattern, not a glob character for pathname expansion. Your shell man page or POSIX spec has all the gory details. – Jens Aug 26 '11 at 14:03

If the shell didn't provide that functionality, then every utility would need to implement their own wildcard functionality (and probably most wouldn't bother doing so). As it is, each utility only has to process individual file names, which is much simpler.

share|improve this answer

Because life would be nightmarishly horrible if it didn't.

share|improve this answer
Ok, I admit this isn't terribly constructive. – Keith Thompson Aug 21 '11 at 21:53

It's the Unix philosophy of "Have one tool do one thing well". Why would anybody want that code repetition in every utility? What's the point of having each and every utility after main() call expand_pathnames()? What's the point of putting that burden not only on C programs, but on all compiled languages and script languages as well? There is no point in all of that. Which is why Microsoft and the cmd.exe abomination got it wrong from day one :-)

BTW, if you want to turn off file name globbing, you can do that with

$ set -f
$ echo *
$ set +f
$ echo *
readme foo.o bar.txt
share|improve this answer
Yes, I got the point. Thanks! – hari Aug 22 '11 at 16:32

You must log in to answer this question.

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