Linux Shell for Beginners: First Terminal Commands
A term that generates a lot of questions among beginners is the Linux Shell. It's true that the Linux universe has a very broad and diverse vocabulary, but understanding each nomenclature is not impossible.
To understand all the points related to the Linux Shell, from what graphical, text, and scripting Shells are, just continue reading this article. Follow along!
What is the Linux Shell?
In simple terms, Shell is nothing more than the prompt or command interpreter that connects the user to the kernel, or operating system.
Its function is to read and interpret command lines and provide a response to the user through operating system outputs.
The name Shell was not chosen at random: similar to the word "concha" in English, in operating systems the shell not only protects the kernel from user errors but also executes functions selectively.
All users can access the Shell and write commands to be executed. From this, the command interface will return feedback to the user according to what was found.
Next, you will understand what the most common Shells are and how each one works, respectively.
Text Shells (CLI)
Text Shells, known by the acronym CLI (Command Line Interface, in English), as the name suggests, are based on command lines, which are represented by text orders.
Graphical Shells
On the other hand, Graphical Shells, represented by the acronym GUI (Graphical User Interface, in English), consist of graphical interfaces, which do not require typed text commands.
This version of Shell is the most common among beginners, since it can be seen in other operating systems, such as Windows, in situations where it is necessary to move, rename, or copy files, for example.
Shell Scripting: understanding Shell programming
This programming modality in Shell greatly facilitates the routine of large companies, which have servers full of files, folders, and directories.
With Shell Scripting, you can automate processes through a set of commands (like a programming code).
Just like in 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 results from Shell, since scripts provide a much more solid structure than simple text command lines.
Advantages and Disadvantages
As you may have noticed, there are many advantages to choosing Shell Scripting for your devices or servers.
The main one is time optimization and obtaining deeper, more complete, and reliable results.
Furthermore, 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 during the day, for example.
On the other hand, the disadvantage is that it can be somewhat complex for beginners in the field who do not have prior programming knowledge.
But don't worry: there are many shell models available for use, you won't need to create one from scratch.
Basic Shell Script creation process
To create a Shell Script, it is necessary to set aside a location to write the lines of code. You can do this by creating a new document either via terminal or graphical option.
Once done, always remember to change the permissions of the created file, adding editor permission; otherwise, it will only be possible to read the document (which doesn't make sense since it would be blank).
Right after, decide which command you want to execute. For this action, just 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 one is bash.
This element allows the shell to interpret and "understand" what you want to execute.
With the code written, it is necessary to save it and exit the command interface. To execute it, simply select your file's directory and paste it into the command prompt and the information will be returned.
This is just a simple, introductory step-by-step guide on everything the Linux Shell can do for you.
But understand that just like any programming language, there are many knowledge stages involved for you to master the Linux Shell with mastery.
To illustrate, there are conditional execution structures in shell, using the "if and then" structure, common in other languages.
Therefore, the best path is to professionalize yourself and seek complete information that makes logical sense, to facilitate your learning curve.
There are many Linux courses excellent, fully well-structured that we at Certificação Linux offer to help you.
Shell for Beginners: working effectively, in practice!
The Linux Shell may seem mysterious. Working with it can be scary at first, but you can learn how to work effectively in the Shell.
Breaking the initial barrier of "code fear" is the first step to being successful in the Linux Shell universe.
It is important to keep in mind what the main characteristics and uses of this tool are, and then understand the command lines and practice.
Just like 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.
Therefore, understand that entering commands in the shell is more than just typing them.
First, the command needs to be valid and be in the directories listed in the PATH variable or with its explicit location.
The command may also require options, usually preceded by the symbol "-" or "–" 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 run. The –l argument can be added to generate a detailed file list:
$ ls –l
We can also add more options to the ls command:
$ ls –l –a –t
Or
$ ls –lta
In the case of the ls command, options can be used separately or combined. The –a option shows all files, including hidden ones.
The –t option shows files sorted by last modification date. The order of options is not important for ls.
If you want to hide a file in Linux, its name must necessarily start with the symbol "." (dot). Ex: .bash_history
For some commands, options must be preceded with two dashes "–" instead of one dash.
Furthermore, some commands offer alternative ways to indicate the same option. In the case of ls, the –a and –all options produce the same effect.
The detail is that options that are called with two dashes cannot be combined.
$ ls –lt –all
And not:
$ ls –lalllt
Some commands may accept arguments as optional. For other commands, arguments are required.
Arguments are the parameters that commands accept or need. 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 which filters files ending with the .txt extension.
Another possible variation are commands that necessarily require an option to perform a task that is usually not preceded by a dash.
It's common for this type of command that its options are followed by arguments. See the dd command as an example:
$ dd if=bootdisk.img of=/dev/fd0
The dd command copies and converts files according to the options passed.
In the example, the command will read an image from a boot disk (option if=bootdisk.img) and will write this image to drive a: (option of=/dev/fd0).
Almost all commands accept the –-help option which shows a simple help of the options and arguments accepted by the command.
Are you on the right path?
It's important that you keep in mind that Linux will only execute commands that are built into the shell, or commands whose location is in the PATH variable, or commands called with their explicit path.
$ ls
Or
$ /bin/ls
In 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 one dash, two dashes, or no dash.
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 to a file called .bash_history located in each user's home directory the history of all commands typed by users.
The possibility of recovering typed commands is fabulous. This can be very useful for audit purposes, to jog your memory, or simply to save your fingers.
You can see the commands you typed 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 files.
Below we have a detailed table of keyboard shortcuts in 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 commands stored in the .bash_history file.
| Shortcut | Description |
| !! | Executes the last command typed |
| !n | Executes the command on line n in the .bash_history file |
| !texto | Executes the most recent command that starts with the text |
| !?texto | Executes the most recent command that contains the text |
| ^texto1^texto2 | Executes the last command replacing text1 with text2 |
| Alt M < | Goes to the beginning of the .bash_history file |
| Alt M > | Goes to the end of the .bash_history file |
| Ctrl p | Retrieves commands stored in the .bash_history file backwards |
| Ctrl n | Retrieves already listed commands forwards |
| Ctrl b | Moves back one character in already retrieved commands |
| Ctrl f | Moves forward one character in already retrieved commands |
| Ctrl a | Goes to the beginning of the line in already retrieved commands |
| Ctrl e | Goes to the end of the line in already retrieved commands |
| Ctrl l | Clears the screen |
| Ctrl d | Deletes characters from cursor to end of line |
| Ctrl k | Deletes text from cursor to end of line at once |
| Ctrl y | Pastes the text deleted by the previous command on screen |
| Ctrl rtexto | Searches for commands containing the text from the last command to the first |
| Ctrl stexto | Searches for commands containing the text from the first command to the last |
This table is a great guide to have at hand when using the Linux Shell.
You can save this page as a favorite in your browser, or share with a friend, to always have easy access for reference!
Conclusion
Did you notice how there are countless possibilities for using the Shell on your Linux system?
Understanding commands may seem complicated, but with a little attention and practice, the features become more user-friendly and easier to work with.
You just need to have clearly in mind all the principles that lead to command execution: 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, easy to understand, accompanied by practical applications and correct instructions, is through the Linux Certification Books, published by Professor Uirá Ribeiro.
There are also courses that are educational and practical about the Linux universe, which will help you understand and apply all the concepts you've learned. It's worth checking out!
That was just one piece. The course teaches the whole path — from your first command to exam day.