What happens when you type in ls *.c in the shell?
Before we get to understand what happens when you type “ls *.c” in the shell, we have got to understand a few programming concepts and some expansions so let’s start from the beginning!
What’s a shell?
A shell is a computer program that presents a command line interface which allows you to control a computer using commands entered with a keyboard instead of controlling it with a graphical user interface (GUI) with a mouse and keyboard as you would normally do with modern computers.
The shell prompt’s
The shell prompt (or command line) is where someone types commands that access the system through a text-based terminal. It is the main way of accessing programs and doing work on the system. Basically, it is a shell surrounding all other programs being run.
But wait, what’s a command?
Don’t worry, we have you got covered too! A command is a directive to a computer program to perform a specific task. It may be issued via a command-line interface, such as the shell, or as an input to a network service as part of a network protocol or as an event in a graphical user interface (GUI) triggered by the user selecting an option in a menu or executing a program.
How about a program? What is that? Can you eat it?
Sadly, no. Technically, you cannot eat it. At least, we wouldn’t recommend trying to. However, we can still tell you what it is! A computer program or program for short is a collection of instructions that can be executed by a computer to perform a specific task. A program is usually written by a computer programmer in a programming language that would suit what that program aims to do.
Let’s recap, shall we?
Now that you understand its basis, we can conclude the following: the shell prints the prompt and once a command was sent or performed, it will print the prompt once again so that you can continue performing tasks over and over endlessly.
The shell and its secrets (Shhh! don’t tell anyone!)
Let’s have a brief look into the shell and some of its secrets, how does it work, the why and how it’s done!
1. As you might have noticed above, the main core of the shell is basically an infinite loop that receives commands over and over.
2. How does it work? Basically, the shell checks if the command you have entered is a built-in function before it checks for a program or executable in the PATH.
3. Wait, built-what…? In computer programs a built-in is a function that was already defined in a program or a framework with a set of statements that perform a task so programmers won’t have to create that specific function and can use it directly in their program or application.
4. Uhm, what’s a PATH? A path is the general form of the name of a file or a directory. It specifies a unique location in the file system. It points to a file location by following the directory tree expressed in a string of characters in which PATH components are separated by a delimiting character that represents each directory.
5. Parsing: the shell will always parse the command you have written into arguments that are read by the its built-ins or passed to the program that’s being executed during that specific command.
6. What’s parsing? In computer programs parsing is the process of analysing a string of symbols that are written in either natural language, computer language or data structures.
7. Processes: one of the biggest secrets of the shell is that it uses the system call “fork” to create a new process that will run a command as without it, the shell would only be able to execute a built-in function or program once. Therefore, an infinitive loop wouldn’t be possible and you would have to run the shell over and over each time you perform a command.
8. Fork and its rots: the system call fork is used to create a new process which is called child process that runs with the current process. It is commonly known as parent process in programming. For instance, after a new child process is created both the child and parent processes will execute the next instruction one after the other so that executables or built-in can be used endlessly without needing to stop the program.
9. Another secret behind the shell is the system call “Overlay Calling Process and Run New Program” or “execve” for short. It is a function is commonly used to overlay a process image that has been created by a call to the fork function which in less words; it allows you to execute a program or script.
10. The next secret of the shell’s is the system call “wait”. It is a function that suspends the execution of the calling thread until you have received status information for one of its terminated child processes or until the program has received a signal whose action is either to execute a signal-catching function or to terminate the process.
11.Aliases: The shell also uses aliases in order to perform tasks easily and that (often) have a more significant name for a defined data object which can be defined later.
12. Another key factor or secret about that shell is the environmental variables which basically are dynamic-named values that affect the way on which running processes will behave like on a computer. They are also part of the environment in which a process is run.
13. Last but not least, another secret of the shell is the environment variable PS1 which changes how the prompt looks like. The PS1 variable changes the shell command prompt appearance and environment so that you can have a little customization on it and it looks something like this!
Here’s how PS1 allows you to customize your promt:
And here’s how the customized version would look like, kind of fancy, isn’t it?
Now that you understand a little bit more about the shell, let’s talk about expansions as by now you should understand how the shell works like. The shell receives some specific metacharacters as in special commands that would do something based on the specific metacharacter you have sent. Metacharacters are also known as wildcard. For instance, The asterisk, star (*). This particular wildcard is used to match any character specified and usually used as a filename expansion. But now, let’s take a closer look, shall we?
Provided we were to type ls *.c in the terminal this specific command would literally list all the files which its extension ends in .c as shown below:
However, if we were to use another command as in; “echo *” it would do kind of the same thing and list every file and directory on the folder you are currently at. Here’s an example:
But what happens if we type “echo *.c” instead? Well, it would literally do the exact same as “ls *.c” it would display all the files ending on a .c extension as following:
Please, notice how it actually does list every file ending in .c.
Now, if we were to discuss what ls -l *.c does, well it’s rather simple. It literally does the same thing but lists the files on long format meaning you are able to see when the file was created, its permissions, its owner and group and obviously the file the name. Here’s an example:
Basically, the shell will execute the function ls and call its executable every time it is used by the user. The shell executes this command by looking into the PATH which as you already, it’s an environmental variable.
Printing the environment
Now, let’s do something more interesting; let’s print the environment using the command “env” so you can have sneaky peak on how it looks like:
And that’s it… Now a last message from our sponsor, ye… Not really but it’s a cool meme!
Written By Juan Jose Carabalí - Carlos Galeano.