4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

What really happens when you Execute a command in the Shell

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
PATHisavariablethatcontainsalistofallthedirectoriesthatthecommandsexistin.TheshellsearcheschecksifthefirstwordisabuiltinthenchecksthePATH 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 thePATH 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.

130 upvotes $0.05

Replies (3)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: