Type ls -l in the shell, and them?

Image 1. Simple representation of a shell as a messenger between the user, the kernel and the machine

What is a Shell?

A Shell is a program that takes command inputs typed from the user’s keyboard and passes them to the machine to run through the kernel. It also checks if the user’s command inputs are correct.
On most Linux systems, a program called bash (which stands for Bourne Again SHell, an improved version of the original Unix sh shell program, written by Steve Bourne) acts as a shell program.

  1. let’s start talking about where the command is written

Prompt, an infinite loop

Image 2. A regular prompt and the prompt of our simple shell.
code 1. Main function of our own simple_shell
image 3. Simple representation of file descriptor .

File descriptor

In Unix, a file descriptor is an abstract indicator (handle) used to access a file or other input/output resource, such as a pipe or network socket. File descriptors form part of the POSIX application programming interface. A file descriptor is a non-negative integer, generally represented in the C programming language as the type int(see image 3):

  1. Standard input: consists of the data that is sent to the program. In most cases, this data is entered using the keyboard or is the result of the execution of a previous command. The file descriptor associated with stdin is 0. As you can see in the code line 25, we read a buffer using getline function with the file descriptor STDIN.
  2. The standard output: is the way the program returns the data after its execution. Usually stdout is the computer screen. The corresponding file descriptor is 1. As you can see in the code line 24, we write our prompt in STDOUT.
  3. The standard error: (stderr, for Standard Error) is the channel through which an error message is sent in case its execution fails. Although this message will generally also be displayed on the screen, it is important to note that Linux allows you to distinguish between stderr and stdout to manipulate both sequences separately. The file descriptor is 2. We also use this descriptor when we write error messages but it is in other functions.

Reading the command

image 4. How to read.

Working with the first argument

Next we will see how to read this argument in search of being able to execute it, our first argument is ls:

  1. Is the first argument a Built-in.
Image 5. Command output

--

--

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