The Shell and “ls -l”

Allen Nicholson
3 min readNov 23, 2020

--

In the Unix operating system ls is a user command. When entered by a user the shell will take the entered command, interpret it, and finally execute the command. This results in a list of the content of the current directory being displayed.

Results when ls is entered in Unix.

The inclusion of “-l” after ls will display a longer detailed list of the contents of the current directory.

The inclusion of “-l” displays more detailed information for the same files that were displayed by entering ls.

The “-l” option displays more information about the files and directories displayed including: permissions, file and group ownership information, file size, date of creation or most recent modification, and the file or directory name.

The shell is what handles the ls -l command when entered, but what is the shell and how does it work? The shell is a program in Unix that takes standard input from users, and then passes it to the operating system to execute. After the command is executed the shell will display the results, and then return to the standard shell prompt ready to accept more commands.

The shell is a command line interpreter that is able to take multiple arguments and operates as an interface between the user and the kernel. It takes commands in and executes the programs that are related to these commands. The shell typically functions in two different modes. Interactive mode, which is what is seen in the example above, and non-interactive mode where commands are piped into the shell. The shell also has built-in options that the user can access, such as exit. If a user enters exit into the shell then it will end the shell process and exit the terminal.

When a command like ls is entered into the shell multiple things occur before the command is executed and the results are displayed. The first thing the shell must do is to check if the command is a function built into the shell, or if it is a command recognized by Unix. Unix has hundreds of programs that can be accessed by commands through the shell, so how is it that the shell is able to find them? The key to this is the PATH.

Example of the PATH being printed using the “env” command.

In Unix the PATH is an environmental variable that holds a list of directories which store various programs used by Unix to execute commands passed by the shell. When a command is entered the shell checks it against all the directories in the PATH, so that when you run a command like ls it will execute regardless of which directory you are currently in. The PATH can be edited to include additional directories, so that even more commands can be accepted by the shell.

A user enters ls -l into the shell prompt. The shell checks to see if the command ls exists by checking built-in commands and then by accessing the PATH to check against the commands in the Unix operating system. The command is found on the PATH, and then the program is executed. The shell displays the results of the command, and returns to a blank prompt awaiting further user input. The magic of the shell works its wonder's once again!

--

--

Allen Nicholson

Student at Holberton just beginning my coding journey.