In this article, I will be talking about what happens when you use the command ls followed by *.c. First and foremost, this is done in GNU Bash. Bash is a command line interpreter opened in a text window and what it does is takes commands from the user and performs the action.
Once a command is done, whether it had a error or successful execution, Bash actually prints the prompt again. The prompt is just the user information, followed by a $ sign and empty space afterwards like this:
When a error is outputted, it looks like this:
And when a command is performs the action successfully, this is what happens:
This next part that I am about to cover on Bash was very heavy learning about it when I researched on it, however it is also important to understand I will do my best to piece it together as short as possible. Environmental Variables come into play and what are they essentially is a class of variables which tells how the Shell should operate when a user is working on the Command Line. There is this environmental variable called PS1. PS1 contains the value of the default prompt. What PS1 shows on the Command Line is the following: username, hostname, working directory, and lastly the user privilege.
And no, it is not Sony’s first video game console, despite the coincidence. I thought so the first I read about it!
Another piece of information about Bash to note is aliases. aliases in Bash are shorthand versions of longer commands. You can actually set an alias for an entire line of different commands and that alias would be just a word or letter that executes said commands you made an alias of. To find out what aliases are on your bash, type alias on the command line and it should list all known aliases. If there are no aliases, the prompt is simply printed again.
So the dollar sign on our prompt does have a purpose and that purpose is to take parameters afterwards. In this case, it takes a multitude of different parameters and that includes something called expansions. There’s one expansion in particular that I will cover since it’s apart of the main topic. That expansion is a special character called the asterisks ‘*’. The asterisks has this special instruction of referencing any character, number, or extension that is typed afterwards. So in this case if we want to look for any file that ends with a .c extension, we just add *.c after writing out ls.
Another piece to explain is PATH and what is it? The PATH is a environmental variable itself and it’s purpose is to search for ready-to-run programs based on the command the user typed on the command line.
Now to bring this full circle, let’s breakdown what happens when we type ls *.c right below:
So the first step the Shell takes in the process is check for aliases, just in case I gave an alias to the entire command. Since I do not have an alias for ls *.c, Bash moves on to checking if the first word of what I typed is a built-in before checking for a program. ls is not a builtin command. In that case, it checks for the program that runs the ls command. Afterwards, it checks if there are any special characters which there is. The asterisks gives the ls command the special instruction to find files in the current directory that end with a .c extension.
Bash checking the parameters on a command line all happen behind the scenes, within the computer. What we humans see is input and output on the prompt; whereas the computer is performing all these actions in the background. It can be lightning fast.
And that is what happens when you type ls *.c on your command line. If you have any questions, criticism or comments, feel free to reach out to me via my email here:
I hope this was informative!