What happens when you type ls -l in the shell

How does it work then?

So let’s begin, when the shell program is opened on your computer the first thing it does is to look for the PS1 environment variable to format the prompt which is where the user is going to input the command.

Open terminal waiting for user’s input
Typed command
input received by strtok()
  • If the executable does not exist the program will return an error, otherwise it will invoke the fork syscall that will create a child to the current process and both programs will continue to execute simultaneously how ever the parent will need to use ‘wait’ syscall to literaly wait for its child to finish before continuing with its own execution, on the other hand the child will need to call ‘execve’ syscall to replace the copy of the parent process (that is created when fork is invoked) with the process that has been requested by the user, in this case ‘ls -l’.
Creating a new process
Result printed on the terminal after typing “ls -l”
  • number of hardlinks
  • names of group and owner
  • file size (in bytes)
  • date/time of last modification
  • file name

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store