What is the function of the asterisk as a standalone command in Unix?

I was messing around in terminal on Red Hat Linux, and when I typed the asterisk (*) followed by return, and it executed one of the programs in my directory. Why?

My best guess is that Unix treated it as a wildcard so it executed the first alphabetic program. Since my_program.exe and one_of_my_programs.program can be executed by simply typing the name of the file, the wildcard operator represents all the possible files. Since a program is first alphabetically, Unix executes it. Is this a correct judgement?

2

2 Answers

Your interpretation is correct. The rest of the files will be presented as its parameter list.

Note that it will do this only if the program has the executable bit set, and the current directory is in the PATH list.

A couple of notes which may help understanding:-

  • If you type ./* then the PATH entry is not a requirement.
  • If you type . * or . ./* and the first matching file is a script, then it need not be executable, nor need the current directory be in PATH (may not be true for shells other than bash).
4

This suggests that . is part of your PATH variable. That is a really bad idea for security reasons (naturally, Windows had to make it an unmodifiable default).

However, this "suggestion" is only mildly valid: if you have a file named rm in your current directory, * will be fine executing the default rm:

/tmp$ mkdir ohno
/tmp$ cd ohno
/tmp/ohno$
/tmp/ohno$ ls
/tmp/ohno$ touch rm what
/tmp/ohno$ ls
rm what
/tmp/ohno$ *
/tmp/ohno$ ls
rm
/tmp/ohno$ 

As you can see, it wasn't rm in the current directory (an empty and non-executable file) that got executed but rather the system's default /bin/rm.

Always double check your commands when wildcards are involved. Here is one of the most terrifying messages to ever read:

rm: cannot remove '.o': No such file or directory

This is the result of calling

rm * .o

, more or less the worst placement of a spurious space one can come up with.

7

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like