Shellton : a shell story

What happens when you type “ls -l *.c” in a shell?

Photo by Alex Knight on Unsplash

Basics of shell

So our shell goes through a few process in order to run. Shell takes inputs from standard input, parses them, and then passes them to the kernel to be executed.

The prompt

First we create an infinite loop in order to run a prompt within it. Our prompt will take in the user input using the getline() function. So typing “ls -l *.c” will store that whole string.

Parsing

Next, using the strtok() function we can parse our user input and create individual tokens that can be stored and interpreted as commands.

Executing

The tokenized string then gets passed through the child process of our shell and ran with the execve() function. This passes the tokenized commands and its arguments to the kernel, then returns to the infinite loop where the prompt will be up waiting for user input.

Conclusion

So, “ls -l *.c” will list all the files that end with .c in our cwd in long format.

--

--

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