0

I stumbled upon this while working through the exercises in K&R2. Why does echo * prints the names of all files in the current directory? More generally, when I write a C program that takes command-line arguments, and when I give it * as an argument, it puts the names of all files in its parent directory in to the argument vector. Why does this happen? What is so special about *?

I could not find anything about this in the internet.

  • 2
    How `echo *` is even related to C? – Eugene Sh. May 23 '18 at 20:56
  • We literally had a question about why `echo` emits glob results rather than the exact string following its name on the unparsed command line [just this morning](https://stackoverflow.com/questions/50488590/bug-in-echo-command-in-linux-shell#comment87990669_50488590). – Charles Duffy May 23 '18 at 20:57
  • If you want to use * as an argument you have to put it in single or double quotes. – Austin Stephens May 23 '18 at 20:58
  • @EugeneSh. I think echo is written in C. If you read the whole question, you will see the link. – Murat Göksel May 23 '18 at 20:59
  • @MuratGöksel, `echo` is a shell builtin, not an external program -- run `type echo` in bash to have the shell tell you as much. Its behavior is thus defined by the shell language specification; it's only a C question insofar as *any* question about something written in C is a C question (and if we accepted that reasoning, everything with the `python` tag would also get a `c` tag, f/e, since the Python interpreter is written in C). – Charles Duffy May 23 '18 at 21:01
  • More to the point, whether `echo` is internal or external, globbing takes place *before `echo` (or any other regular command) is even started at all*. – Charles Duffy May 23 '18 at 21:08
  • @MuratGöksel, unless your platform is Windows, your program has no control whatsoever about how arguments are expanded before that program is invoked. Expansion is done *by the shell*, outside the program's control. – Charles Duffy May 23 '18 at 21:09
  • @MuratGöksel, ...it also looks like your *real* question (after editing) is whether you can prevent or avoid that expansion. If so, we have quite a number of duplicates on the topic. – Charles Duffy May 23 '18 at 21:10
  • ...for example, [Stop shell wildcard expansion?](https://stackoverflow.com/questions/11456403/stop-shell-wildcard-character-expansion), which asks *Is there any way for a compiled command-line program to tell bash or csh that it does not want any wildcard characters in its parameters expanded?* – Charles Duffy May 23 '18 at 21:10
  • ...the important to thing to understand is that programs aren't (at the operating system layer, in UNIX-family systems) given a string with arguments -- the argument vector is passed by the calling program *in vector form*, as you can see in `man execve`. Conversion from a string to a vector is done by the shell, if there *is* a shell -- more reliable ways to start a subprocess with a precise argv don't use one. If you want to control how the shell does that conversion... well, that's what quoting (like `echo '*'`) is for. – Charles Duffy May 23 '18 at 21:13
  • ...now, in *Windows*, the argument list is passed a string, and while the local libc has default parsing available, programs have control over how they parse/expand it... but Windows is a very different beast (and has a ton of inconsistencies on account of this decision, making it very difficult to pass arbitrary data between programs in a safe and reliable way). – Charles Duffy May 23 '18 at 21:17

1 Answers1

1

This is called globbing. Here's a detailed description. Other wildcards include ? for one character, [abc] for one of a set of characters, and [a-z] for one of a range of characters. This is built into various shells, including Bash.

In response to your comment "I think echo is written in C" — this doesn't matter a bit. Once source code is compiled into an executable containing machine code, it doesn't matter what language it was written in.

Luke
  • 7,110
  • 6
  • 45
  • 74
  • Please make an effort to check for duplicates before answering questions like this -- see [How to Answer](https://stackoverflow.com/help/how-to-answer), particularly the 3rd bullet point in the "Answer Well-Asked Questions" section. – Charles Duffy May 23 '18 at 21:03
  • I think I phrased it poorly. Please refer to the part in bold. – Murat Göksel May 23 '18 at 21:10
  • If you're invoking your C program on the command line in a shell like Bash, my answer still applies. – Luke May 30 '18 at 12:41