Who deals (interprets) the * in
Does the echo see the star or the shell take care about it and return a list of filename ..
cp temp temp*
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
bash (or whatever you use as shell), is the first thing to read any input, and will start interpreting special characters such as
* 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:
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,
echowill literally just echo anything bash passed to it as arguements.
cp temp temp*will behave somewhat like the
mvcommand 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
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
echo * isn't then going to behave like a poor man's
ls as the former will print nothing while the latter will print
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
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
ls * is a bit of a cliche
* 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.
The shell performs several expansions before the arguments are handed to the command.
Not bash-specific, see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_01