What happens when you type `ls -l *.c` in the shell
Cameron Jones-Crupper
Driven junior software developer skilled in multiple programming languages, eager to apply my problem-solving abilities and innovative mindset to create efficient and cutting-edge solutions, while continuously learning.
A shell is a non-graphical user interface.?It is a command line interpreter.?What exactly does that mean??A normal modern interaction with a computer involves lots of pretty pictures of files for you to click on.?You are able to see exactly everything that is available to you at the current level of a folder you are on.?In order to move into that folder you would click on it and then immediately be taken into said folder and be able to see everything within that folder.?A shell or command line interpreter is a much more basic way of doing this.?It has one command line where you would type an available command such as “ls -l *.c” and it will return whatever that command instructs it to do.?The “ls -l *.c” is actually a mix of several basic commands which lets you be way more specific with what action you are trying to accomplish.?Where as with a GUI a (graphical user interface) you would basically be just moving from folder to folder seeing everything that’s available which can sometimes be a lot of information depending on what you working on or looking into. A shell can do specific task such as only listing certain files that end in .o .c .whatever.
???????????????In order to use a shell you will need to use a terminal emulator. There are several of these emulators available and on all systems they come standard but are rarely used by everyday computer users.?On windows there is Command Prompt, on Apple there is the Terminal App, and there are many others such as Xshell, FireCMD, MobaXterm, Cmder.?These all will facilitate the shell that is used today which is bash. Bash is used in Unix which is an operating system.?Bash stands for “Bourne again shell” as it is based on the original program which is called Bourne shell.
???????????????How exactly a shell works is still a mystery to this day.?Just kidding, it is fairly simple to explain. It starts at the operating system which is software that interacts with the computers hardware. It is broken down into two different parts that are called the kernel and the user space. The kernel is responsible for interfacing with physical devices, managing resources that are available for users and programs, starting up the various systems that are needed among other things.?The kernel maintains balance between the user and resources that programs use. Kernel and shell are used literally where shell would be the hard outside and kernel the soft inside. When a program starts it is allocated a private segment of memory and a limited amount of access to resources. Programs have access to a library of functions by the operating system.?Everything in the user space is in whats know as a sandbox. A virtual area within an operating system that can be destroyed to limit any damage that could actualy be done to a computer.?A program running in the user space can run fprint which is a function provided in almost all operating systems in the standard C library of functions and allows it to open a file.?If the program is allowed to open the file which is determined by things like the ability to read, write, or certain permissions, then it will be given access and allowed to open the file.?This is a system call in the kernel.?This will then display the file on the computer monitor.
???????????????The shell which is where you would type out what ever command you are wanting access to is a name for any user space program that allows access to resources in the system. So from everything that we have gone over so far this is when you would type in your first command which is ls with the option of -l added to the end. Ls -l will display all files AND directories within the current working directory.?Ls itself will display all the files and all directories and the -l option will show all permissions, owners, and dates the files and or directories were created. ?This is everything that you would see that happens on the surface but there are things that take place below the surface to make this command part operate.?First off the shell will check your PS1 variable in the environment variable and it will use that value to create the shell prompt.?PS1 does not stand for Play Station 1, it stand for prompt string 1 which is the primary prompt string. This is what is displayed before your command line which is the info of who logged in, on what machine the person logged in on , what the present working directory the person is in and is the user a super user or a normal user. Then after the PS1 is the command prompt which is usually either a # or more commonly a $.?Once the user presses the enter key on the hardware the shell then takes over and reads the command that was entered.
领英推荐
???????????????The shell will then use a function called a getline() function to read a line from the standard input which is the command line and store it in what is called a buffer.?Then a function called strtok() short for string token will part or tokenize the individual strings and separate it with a space which is know as a delimiter.?The string token function will be called multiple times and stores a pointer in a static variable where it was last located.?Once the shell has read the ls -l command from getlines standard in it will parse the line?into separate arguments that is then given to the program that is executing it.?Next up shell will check if any aliases have been added to the ls -l command.?If an alias has been entered the shell will expand the alias before the command and see if the command is a built-in command. Then it will try to detect any special characters such as our * and run the options associated with it.?The star wildcard has one of broadest meanings in all of the wildcards.?It can represent all single characters, or no characters, or any string.?Our command ls -l *.c uses it to represent any string, which means that the system can use it to return information about every object in whatever directory is specified.?Our command joins the star with the .c command, so when used together will display information about every object in the current working directory that ends in .c.?So the full command will display all files AND directories within the current working directory displaying all permissions, owners, and dates the files and or directories were created for every file in the current working directory that ends in .c.?
???????????????Once this happens the shell checks for the program file and see if the first word is a built function before it checks for a program in the PATH.?The $PATH variable is a list of directories the shell searches every time a command is entered. $PATH, one of the environment variables, is parsed using the ‘=’ as a delimiter. Once the $PATH is identified, all the directories in $PATH are tokenized which are then parsed further using ‘:’ as a delimiter.?Shell will then check each directory that is separated by a : to see if the ls command is in those directories. If the command does not exist then an error message will output stating that the command is not available.?To execute the single ls command three different system calls are made.?Fork, execve and wait.?In order for the shell to create a new process the system call fork() is made to create a duplicate of the parent process.?This newly created process is called a child process.?Now that there a two different processes running the same thing execve() system call is made.?Once this happens the operating system stops the parent process and loads in ls.?The execve() system call will replace the current process with the new program from ls’s executable file.??
???????????????There is an important part of the child process that is using the wait() command.?It waits for the command process to be completed before it exits back to the command prompt in shell. If ls is executable shell then uses the execve() system call to run and execute the program from the child process.?After the ls -l *.c command executes the shell will use shutdown commands, free memory, exit, and then ask the user for their next input.?If the command that was typed can not be found it will issue an error message and then prompt the user for a valid command.?
???????????????All of these things need to happen in order for the shell to complete its action and checks for said files in the current working directory. It helps to know exactly what is happening behind the scenes to build your own simple shell.??