Who deals with the star * in echo *

Who deals (interprets) the * in

echo *

Does the echo see the star or the shell take care about it and return a list of filename ..

What about

cp temp temp*

bash (or whatever you use as shell), is the first thing to read any input, and will start interpreting special characters such as ? and *. * gets expanded to whatever matches in the CWD, which means that the asterisk is substituted by said matches.

In most cases, this is fairly straght forward, but can lead to some confusing cases from time to time.

Consider the following. A directory has this contents:

  • test (regular file)
  • test1 (directory)
  • test2 (directory)
  • test3 (directory)

If you then type mv * something seemingly weird happens: test3 is there, but the rest is gone. While weird at first, it makes sense once you understand what bash actually passes to mv. Because of the asterisk, bash interprets mv * as mv test test1 test2 test3, and when mv gets that list, it'll assume that the last arguement is the destination, which is where all of the files would've been moved.

As for the commands you listed:

  • echo * can function as a poor-mans ls. The shell will expand the asterisk to whatever is in that directory, and as I'm sure you already know, echo will literally just echo anything bash passed to it as arguements.
  • cp temp temp* will behave somewhat like the mv command I described above, unless there's only one directory named temp, in which case source and destination name is the same, i.e. it'll do nothing.

As already stated, the shell expands * so echo receive as arguments whatever the shell find in the current directory. However, note that if the expansion leads to nothing, i.e. in that case if the directory contains no non-hidden files, the * is left unchanged and passed as is to the command called (unless non standard options are used with some shells like bash.) echo * isn't then going to behave like a poor man's ls as the former will print nothing while the latter will print *.

Similarly, cp /tmp/temp temp* will create a file named temp* in the current directory if there not yet at least one file which name starts with temp.

Finally, if you want the * to be passed unchanged whatever the case, you can protect it from expansion using either single quotes '*', double quotes "*" or backslash \*.


In Bash, the shell deals with it. You see that if you even try * without echo

Note- based on some comments, I would suggest when running * ENTER, to create a directory and use the touch command to make some files, and make sure none of them , or at least make sure the first one alphabetically, is not the name of any script or command in the path.

$ *
bash: a: command not found

$ echo *
a a.aa a.ab a.b a.htm a.tx

So ls * is a bit of a cliche

In Windows, * is handled by the command, so dir *.* is not a cliche.

Note- Seeing some comments, I would add, there is a risk running * then ENTER. If you have a file called rm that is first in the directory listing, then it's dangerous because anything after it would get deleted. Also, and this is less unlikely, if the first file in the directory listing is the name of a script in the path, then it will run that.