What happens when you type ls -l on the command line?
Before we answer the question, let?s take a deeper look into the shell. What is a shell? A shell is simply, the command line. The shell is a program that takes a keyboard command like ls -land passes it to the OS (operating system) to carry out. The user should understand that the file system resembles a file system tree. Files are organized in a tree like shape pattern of directories.
?Unix-like operating system such as Linux organizes its files in what iscalled a hierarchical directory structure. (The Linux Command Line, William Shotts)
The shell is a textual representation of the file system on your computer. So, a graphical interface in contrast uses icons of folders to represent the files they hold. The user can double click the folders to gain access to the files.
Graphical user interface (GUI) in a Mac OS
An everyday user like you and I would interact with our computer using a GUI. There is also, another way to interact with your computer and that is typing commands like ls -l in the shell. The ls -lcommand is the most used bash command. It is a helpful tool that allows navigation around the shell. The user interacts with the shell by opening the computer?s terminal. The user types in commands to navigate through the file system. The shell waits for the user to invoke a command. It waits for you ? the pilot. Ultimately, you have the luxury and the power to explore your system in any way that you desire.
A terminal, with the command ls -l typed in the shell.
Don?t be afraid. Don?t let the simplicity fool you either. The command line is a direct interaction with the computer system. There are so many commands and tools the user can utilize, which makes the shell a powerful tool.
The simple command of ls -lmeans, to list files and directories. It has an option of -l, which lists the contents in a long format like the picture on the left. It allows you to look through the file system. When you type commands like ls on a keyboard, the shell ? a program executes the commands. Remember, it?s how the user interacts with the OS. On most Linux systems, the default shell is called bash.
There?s a lot to unpack there. So, let?s start with answering the question: What happens when you type ls -l on the command line?
Let?s go through the steps:
- Type the command, $ls -land hit the enter button on the computer to execute the command.
- A new line will attach to the end of ls -ln
What? Ok? back up.
It?s all in the details. When the command ls -l is entered, the new line nis cut from the end of the command. A null byte, ”takes its place and specifies the end of the command. The null byte is important because it lets the computer know how many commands and flags to execute.
A powerful function called strtok()parses the command. The command is now a character string that is saved into an array of strings. The function separates the lsand -linto their own designated spots. It is concatenated. Keep note: the original command of ls and the concatenated string of ls and -l will be saved. This is an important detail.
Most bash commands entered by users will come come across the PATH when operating the shell. First, before a command enters the path, the shell will check if the command falls under a built in command or an alias. If the command follows either case, the computer will push the command to enter another process.
If the command is neither like ls -l, then the command will be compared to the PATH. What is the PATH? It is a string of directories and they are separated by colons.
What do these directories hold? They hold most of the commands that a user can enter.
When the user types in ls -l, the command goes through the PATH.
It looks for the location of ls in the PATH and the path for this command is /bin/ls.
To search through these directories:
- the shell needs to find the correct variable, PATH. It is part of the shell?s environment and shell has many variables.
- The shell will have to search through the environment?s variables until it finds PATH.
- When the PATH is found, it is separated by the strtok ( ) function and it is parsed. It will also, search for colons (:) ? the directory, instead of spaces.
- The PATH and the colons are saved into an array of strings.
Almost done? and time to string compare
The shell now has an array of strings that contains all of PATHs directories. Sweet! So, ls should be attached to /bin.
What does shell do?
It will search through the list of directories.
Append ls to each directory.
Check to see if it exists and if it does, then it executes the command!
In detail, shell goes through the array of strings and when it comes across the /, it will concatenate, then ls. It will go through a check of each directory?s path to verify if the file exists. If the file exists (yay!), it will return the concatenated string, /bin/ls.
Alright, does the shell sound pretty amazing? It gets better. So, the shell has been running one process and it?s pretty much doing all the work. It will now create an identical child process to execute the ls -l. The parent process (the original) will wait until the child process is done ? whether it will execute the command or return an error message.The shell now has the full path of the executable file, /bin/ls and now, it will attach the flag, -l. The execution is ready to complete and if successful, it will display the file contents of the directory in long format. In completion of the process, it returns the parent process, which prompts the user to enter another command. The return to the prompt allows the user to enter more commands and look around the shell.