What really happens when you Execute a command in the Shell

in #bash5 years ago (edited)

So, you’ve started using bash or maybe you’d like to start. Maybe you’re struggling with what the shell really is. On the other hand, you might have a vast knowledge about BASH but maybe you’d like to know more about what going on inside when you enter those commands. If you have any of these issues then hopefully by the end of this they will be resolved.

BASH & THE SHELL
BASH = BournAgainSHell
Bash is a shell: which means it is a program that allows for a user to type in commands from a keyboard that the operating system performs. Shells are run on terminals which is basically a program that allows you to access the shell.

COMMANDS AND HOW THEY WORK
The example command we will use is ls *.c

"ls" is a command for listing the files and directories in a directory. When you enter the command “ls” by itself it’ll only display the files and directories in your current working directory.

Screenshot from 2019-07-24 03-48-43.png

Like other commands “ls” has certain parameters or options that you can add that allow it to do more. An example of this is "ls -a", this code allows for you to list all files and directories including the hidden ones (files that start with a ‘ . ’).

Screenshot from 2019-07-24 03-52-28.png

THE MAN PAGE
You can see more about commands by using the man page for it or if its a builtin you use the help page for it.
rtfm_sticker_rectangle.jpg

THE WILDCARD
The "*" or asterisks is used as a special character for selecting everything in the directory, "*.c" represents every filename that has a ".c" at the end of it. So "ls *.c" displays every file that ends with .c in the directory. These special characters are called expansions.
For more info about the asterisks check out: http://tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm

Screenshot from 2019-07-24 03-58-15.png

ALIASES
Another feature to the shell is that you can create an alias which is basically creating a custom name for a command or group of commands. Allowing you to access the command(s) with the alias you gave it. aliases are very important because the shell looks through them before looking through the PATH.

BUILTINS & COMMANDS
One type of command you’ll use is called a builtin, these commands are built into the shell itself. These commands include the commands type, cd, and pwd to name a few. You can use the parameter - -help to learn more about builtin commands. The other type of commands that are not builtin are executable files that are located in $PATH.

THE PATH
$PATH is a variable that contains a list of all the directories that the commands exist in. The shell searches checks if the first word is a builtin then checks the $PATH for the program.

SYSTEM CALLS
When you type a command in the command line and enter it, the shell has to do three system calls (tells the kernel to do something), before its fully processed. It gets forked first, forking is when your shell's parent process creates a child process then EXEC() basically runs the command then WAIT() waits till the child process finishes and prints the prompt held in the environmental variable PS1 again.


THE ENDING
Using a bash terminal is what I've been spending a lot of my time doing since I started learning how to become a software engineer at Holberton. It makes things so much easier, I've used it to the terminal to create programs in c, python, and bash. I hope you learned at least a little bit from my experiences with bash.

Thanks for reading my article on basic shell concepts, if you like this content please let me know. Please make sure if you leave feedback.

Sort:  

Hello chriscodes, welcome to Partiko, an amazing community for crypto lovers! Here, you will find cool people to connect with, and interesting articles to read!

You can also earn Partiko Points by engaging with people and bringing new people in. And you can convert them into crypto! How cool is that!

Hopefully you will have a lot of fun using Partiko! And never hesitate to reach out to me when you have questions!

Cheers,
crypto.talk
Creator of Partiko

Hi @chriscodes

As a follower of @haccolong this post has been upvoted by @hoaithu's Curation Trail.
This is free upvote first when you follow @haccolong

Use Partiko for activities on Steemit, you will receive bonus points. Dowload Partiko app for your phone at here. If you difficulty when convert points (Partiko) to upvote, check the instructions here.
cry_e.gif

Congratulations @chriscodes! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made your First Vote
You published your First Post
You got a First Vote
You received more than 10 upvotes. Your next target is to reach 50 upvotes.
You made your First Comment

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Vote for @Steemitboard as a witness to get one more award and increased upvotes!