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

21

3

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?

user6086585

Posted 2017-10-04T13:31:01.710

Reputation: 219

I think not all shells sort the expansion of * alphabetically, but bash is one that does. – aschepler – 2017-10-04T22:04:07.923

1

@aschepler: all POSIX-conforming shells must; see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13_03 para 3.

– dave_thompson_085 – 2017-10-05T08:43:22.913

Answers

20

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).

AFH

Posted 2017-10-04T13:31:01.710

Reputation: 15 470

10"it will do this only…" – It can be more interesting. If there's a shell function, builtin, or an executable earlier in PATH with the same name as the first file then this other command will be executed. mkdir foo; cd foo; touch rm xyz; ls; *; ls. – Kamil Maciorowski – 2017-10-04T14:33:33.420

@KamilMaciorowski - Fair point: my statement "it will do this only…" specified necessary conditions, but not sufficient ones. The command will also behave differently if * is an alias. – AFH – 2017-10-04T15:00:36.410

1Conclusion : don't do this ! Even if you think you can rely on glob expansion alphabetic order, note that this order depends on the locale. – Aaron – 2017-10-05T09:52:53.243

in addition to . * or . ./* one can bash * (or any other shell). – Olivier Dulac – 2017-10-05T10:27:01.417

11

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.

user777455

Posted 2017-10-04T13:31:01.710

Reputation:

1It doesn't hurt to create a function rm() which either adds -i to the parameters or checks the parameters and asks for confirmation if there are more than a certain number. – AFH – 2017-10-04T22:00:55.903

3+1 for "Here is one of the most terrifying messages to ever read". – user541686 – 2017-10-05T00:34:02.910

+1 for "That is a really bad idea for security reasons (naturally, Windows had to make it an unmodifiable default)." – Duncan X Simpson – 2017-10-05T06:01:50.573

FTFY: mv /tmp/ohno /tmp/ohnoes (https://www.google.fr/search?q=ohnoes&source=lnms&tbm=isch)

– Olivier Dulac – 2017-10-05T10:30:07.920

There's a good reason why Windows made it an unmodifiable default. It has to maintain a chain of backwards compatibility from the days before DOS implemented directories. Bear in mind those days nobody using a PC had a hard disk, and floppy disks were small enough that directories weren't initially considered necessary. – Muzer – 2017-10-05T14:17:47.087

. isn't necessarily in the PATH. Could coincidentally have the cwd be something else in the PATH. – Kat – 2017-10-05T17:51:05.643

“more or less the worst placement of a spurious space one can come up with”? No, a couple of years ago somebody meant to type rm ./* and instead typed rm . /* – Scott – 2017-10-23T06:17:04.067