Linux Shell for Beginners
A term that raises a lot of questions among beginners is the Linux Shell. It is true that the Linux universe has a very wide and diverse vocabulary, but understanding each nomenclature is not impossible.
To understand all the points related to the Linux Shell, including graphic, text and scripting shells, just continue reading this article. Follow up!
What is the Linux Shell?
Simply put, Shell is nothing more than the prompt or command interpreter that unites the user with kernel, or operating system.
Its function is to read and interpret command lines and provide a response to the user through the operating system’s outputs.
The name Shell was not chosen at random: similar to the word “shell” in English, in operating systems the shell not only protects the kernel from user errors but also performs functions selectively.
All users can access the Shell and type commands to be executed. From this, the command interface will return feedback to the user according to what was found.
Next, you will understand which are the most common Shells and each one of them works, respectively.
Text Shells (CLI)
Text Shells, known by the acronym CLI (Command Line Interface), as their name suggests, are based on command lines, which are represented by text orders.
Graphical shells
On the other hand, Graphic Shells, represented by the acronym GUI (Graphical User Interface), consist of graphical interfaces, which do not require typed text commands.
This version of Shell is the most common for beginners, since it can be seen on other operating systems, such as Windows, when it is necessary to move, rename, or copy files, for example.
Shell Scripting: understand Shell programming
This type of programming in Shell greatly facilitates the routine of large companies, which have servers filled with files, folders, and directories.
From Shell Scripting, you can automate processes through a set of commands (such as a programming code).
As in the C language, for example, it is possible to customize commands with conditions, repetitions, and logical structures.
This is a good option for those who want more complete Shell results, since scripts provide a much more solid structure than simple text command lines.
Advantages and Disadvantages
As you can see, there are many advantages to choosing Shell Scripting for your devices or servers.
The main one is the optimization of time and obtaining deeper, more complete and reliable results.
In addition, it is with scripting that user activity can be controlled and monitored more easily and efficiently. There are specific command blocks to check which users sent the most emails on the day, for example.
On the other hand, there is the disadvantage of being somewhat complex for beginners in the field who have no prior knowledge of programming.
But don’t worry: there are several shell templates available for use, you don’t have to create one from scratch.
Basic Shell Script creation process
To create a Shell Script, it is necessary to reserve a place to write the lines of code. You can do this by creating a new document either via a terminal or a graphic option.
Once this is done, always remember to change the permissions of the created file, adding the editor permission, otherwise it will only be possible to read the document (which makes no sense, since it would be blank).
Then decide which command you want to execute. For this action, simply consider what is the greatest need in your organization’s routine.
With the command code in hand, select the file interpreter to be used. The most common is bash.
This element allows the shell to interpret and “understand” what you want to execute.
Once the code is written, it is necessary to save it and exit the command interface. To run it, simply select the directory of your file and paste it at the command prompt and the information will be returned.
This is just a simple, introductory step by step on everything the Linux Shell can do for you.
But, understand that just like any programming language, there are many stages of knowledge involved in order for you to master the Linux Shell.
To illustrate, there are conditional shell execution structures, using the “if and then” structure, common in other languages.
Therefore, the best path is to become professional and seek complete information that makes logical sense to ease your learning curve.
There are many excellent, fully structured Linux courses that we at the Linux Certification offer to help you.
Shell for Beginners: working effectively, in practice!
The Linux Shell seems mysterious. Working with it may be daunting at first, but you can learn how to work effectively in Shell.
Breaking the initial barrier of “fear of codes” is the first step to success in the Linux Shell universe.
It is important to keep in mind what are the main features and uses of this tool and then understand the command lines and practice.
As in many other areas, IT also requires a lot of practice after understanding the basics of theory. The secret is to understand and apply, until you find the expected results.
So, understand that entering commands into the shell is more than simply typing them.
First, the command must be valid and be in the directories listed in the PATH variable or with its explicit location.
The command may also require options, generally preceded by the “-” or “-” symbol and arguments.
The important thing is to know that each command has its own unique syntax and there may be variations depending on the Linux distribution.
$ ls
This command lists the files in the current directory. It doesn’t require any options or arguments to execute. The —l argument can be added to generate a detailed file list:
$ ls —l
We can also add more options for the ls command:
$ ls —l ——t
Ou
$ ls —lta
In the case of the ls command, the options can be used separately or combined. The option —shows all files, including hidden files.
The —t option shows the files ordered by the last modification date. The order of the options is not important for ls.
If you want to hide a file on Linux, its name must necessarily begin with the symbol “.” (dot). E.g.: .bash_history
For some commands the options must be preceded with two “—” dashes instead of a dash.
Also, some commands offer alternative ways to indicate the same option. In the case of ls, the options -a and —all produce the same effect.
The detail is that options that are called with two traits cannot be combined.
$ ls —lt —all
And no:
$ ls —lalllt
Some commands may accept arguments as optional. For other commands, arguments are required.
Arguments are the parameters that the commands accept or require. Let’s look at the ls command:
$ ls —l *.txt
In the example above, the ls command received the —l option and the *.txt argument that filters files ending with the extension .txt.
Another possible variation are the commands that necessarily require an option to perform a task that is generally not preceded by a trace.
It is common for this type of command for its options to be followed by arguments. Take the dd command as an example:
$ dd if=bootdisk.img of=/dev/fd0
The dd command copies and converts files according to the options given.
In the example, the command will read an image from a boot disk (if=bootdisk.img option) and save this image to the drive at: (option of=/dev/fd0).
Almost all commands accept the —-help option, which shows simple help from the options and arguments accepted by the command.
Are you on the right path?
It is important to keep in mind that Linux will only execute commands that are internal to the interpreter, or commands whose location is in the PATH variable, or commands called with their explicit path.
$ ls
Ou
$ /bin/ls
During the exam it is very important that you are well familiar with the commands listed in this chapter and their syntax.
Therefore, you need to know when the command accepts options with a dash, two dashes, or no dashes.
Bash also allows you to enter a sequence of commands on the same line. To do this, you must separate the commands with the; symbol (semicolon).
$echo $PS1; echo $PS2
Bash writes in a file called .bash_history located in each user’s home directory the history of all commands entered by users.
The ability to recover the typed commands is fabulous. This can be very useful for auditing purposes, recalling memory, or simply saving your fingers.
You can view the commands you entered by viewing the contents of the .bash_history file.
$ cat ~/.bash_history
The cat command, among other things, is used to view the contents of the files.
The following is a detailed table of the shortcuts in the Linux so you can make quick use of the commands stored in the .bash_history file.
The “~” symbol refers to the home directory of the logged in user.
TABLE — Quick use of the commands stored in the .bash_history file.
Shortcut | Description |
!! | Execute the last command typed |
! | Run the command on line n in the .bash\_history file |
! | Run the most recent command that starts with the text |
!? | |
Execute the most recent command containing the text | ^text1^text2 |
Execute the last command replacing text1 with text2 | |
Alt M < | Go to the beginning of the file .bash\_history |
Alt M > | Go to the end of the file .bash\_history |
Ctrl p | Retrieve the commands stored in the file .bash\_history from back to front |
Ctrl n | Retrieve the commands already listed from front to back |
Ctrl b | Return a character to the commands already retrieved |
Ctrl f | Go one character Forward on the commands already retrieved |
Ctrl a | Return to the beginning of the line in the commands already recovered |
Ctrl and | Go to the end of the line in the commands already recovered |
Ctrl l | Clear the screen |
Ctrl d | Delete characters from the text of cursor to the end of the line |
Ctrl k | Delete the cursor text to the end of the line at once |
Ctrl y Paste | the text deleted by the previous command on the screen |
Ctrl rtext | Search for commands containing the text from the last command to the first |
Ctrl stext | Search for commands that contain the text from the first command to the last |
You can save this page as a favorite in your browser, or share it with a friend, so you always have easy access for consultation!
Conclusion
Did you notice how there are countless possibilities for using the Shell for your Linux system?
Understanding the commands may seem complicated, but with a little attention and practice, the resources become more user-friendly and simple to use.
It is enough to be very clear in mind all the principles that lead to the execution of the command: path, logical structures, functions, and expected return.
In this case, always seek information from reliable sources published by professionals who truly know the field and have worked in it for a long time.
A good way to find reliable information, simple to understand, accompanied by practical applications and correct instructions, is through the Linux Certification Books, published by Professor Uira Ribeiro. There are also didactic and practical courses about the Linux universe, which will help you understand and apply all the concepts learned. It’s worth checking it out!