What Happens When You Use a Wildcard * at the Shell Prompt

Let us look at “ls *.c”

What happens behind the scenes when we use a wildcard (*)? If you’re curious, read on…

vagrant@vagrant-ubuntu-trusty-64:~$

Above is a shell prompt, ready to accept input. The default appearance is usually your username@machinename, followed by the current working directory, and a dollar sign.

Caution: If you see a # sign instead of a dollar sign, that means you are logged in as root, the superuser, with administrative privileges. Be cautious since you are able to delete or override any file on the system. Unless you need administrative privileges, do not operate as the superuser.

If you don’t want your shell prompt to have a dollar sign, you can also customize it and set it to something else with the environment variable PS1, used for the format of the prompt. You can change it from $ to > or to almost anything you like. For now, just know that you can customize the PS1 environment variable.

Let’s explore “ls *.c” in more details, and to do that, we’ll define aliases, builtins, expansion, and the PATH.

Alias is a command that you can define yourself, based on other commands.

The “ls” command is an alias for the ls command with the color option added. This is why when you type ls, the directories and files you see are colorized according to their type.

Builtins are commands built into the shell itself. Bash provides some commands internally called shell builtins.

The cd command and the echo command are both examples of shell builtins.

Expansion happens when we enter a command at the shell prompt and press enter, and shell performs several processes upon the text before it carries our command.

For example, * is a wildcard that matches any character.

ls *.c

In pathname expansion, if we type “ls *.c” at the command prompt, the * is expanded to match all filenames that end with .c (the extension for C source files).

The order of expansions is as follows: brace expansion, tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, word splitting, and filename expansion.

PATH is an environmental variable that tells the shell where to search for an executable file.

To see what your path is currently set to, type echo $PATH at the shell prompt and hit Enter.

vagrant@vagrant-ubuntu-trusty-64:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

The PATH shows all the locations that the shell will search, with each location separated by a : (colon).

Now that we understand aliases, builtins, expansion, and the PATH, we can finally talk about what happens when we type “ls *.c” at the shell prompt?
vagrant@vagrant-ubuntu-trusty-64:~$ ls *.c

The shell will first look at aliases before looking at builtins and commands in the PATH.

The shell will also look if the first word of a command is a builtin before checking for a program in the PATH.

With the * wildcard, thanks to expansion, it will match all filenames that end with .c and list them. Then it will print the prompt again.

vagrant@vagrant-ubuntu-trusty-64:~$ ls *.c
lakes.c oceans.c rivers.c
vagrant@vagrant-ubuntu-trusty-64:~$

Now you understand more about wildcards. Have a wild time programming!