Linux Shell for Beginners: Basic Command Line Commands
It is important to say that even with a beautiful, practical, and diverse graphical environment, the Linux's strength lies in the command line. For advanced users, it is in the system operating system shell that the magic happens, because Linux allows the user to do whatever they want, with the ease of editing a file. The concatenation of commands also allows creating true computational Swiss Army knives.
Accessing the terminal is simple and quick. Simply search for the Terminal application in the graphical environment, or press the key sequence Ctrl-Alt-F1...F2...F3.. to access the terminal.
Linux Shell for Dummies: The Command Line
Entering commands in 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, usually preceded by the "-" or "--" symbol and arguments. The important thing is that each command has its own unique syntax and there may be variations depending on the Linux distribution.
Let's look at some simple examples with the ls command:
$ ls
This command lists the files in the current directory. It does not 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 –a –t
Or
$ ls –lta
In the case of the ls command, options can be passed 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.
To hide a file in Linux, its name must necessarily start with the "." (dot) symbol. Ex: .bash_history
For some commands, options must be preceded with two dashes "--" instead of one dash. Also, 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 called with two dashes cannot be combined.
$ ls –lt --all
And not:
$ ls --lalllt
Some commands can accept arguments as optional. For other commands, arguments are required. Arguments are the parameters that 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 .txt extension.
Another possible variation is commands that necessarily require an option to perform a task that is usually not preceded by a dash. It is 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 the a: drive (option of=/dev/fd0).
Almost all commands accept the --help option that shows a simple help of the options and arguments accepted by the command.
It is important that you 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
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. For this, you must separate the commands with the ; (semicolon) symbol.
$ 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 quite useful for audit purposes, remembering memory, or simply saving 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.
![]() | The "~" symbol refers to the logged-in user's home directory. |
Shell Variables
The user's first contact with the Linux shell is the command prompt.
How this command prompt presents itself can change in each distribution, from the simplest, with a simple "$" or with user information, server name, and even the current directory:
[uira@server1 ~]$
The command prompt can be customized through the configuration of a variable that the Linux shell maintains called PS1.
Variables are loaded at the beginning of bash execution and can also be configured manually at any time.
The first variable we will discuss is the PS1 or simply Prompt String 1.
This variable stores the content of the bash command prompt when it is ready to receive commands.
There is also the PS2 variable that stores the content of the command prompt when multiple lines are needed to complete a command.
These two shell variables do not affect how the interpreter will process received commands, but they can be of great help when carrying extra information such as username, current directory, etc.
It is conventional that variables are all written in uppercase. But it is important that you know that $name and $NAME are two different variables for the shell.
The content of any Shell variable can be viewed with the echo command followed by the $ symbol plus the variable name:
$ echo $PS
\$
The \ character says that any other character that follows it must be interpreted exactly as it is and not processed by the shell. It's what we call a metacharacter.
It's common for the default bash command prompt to come in the format: \u@\h:\W\$
Commands and variables in Linux are case-sensitive. Each of these characters is interpreted in a special way.
The \u is used to represent the username, the \h is used to represent the system name (hostname) and the \W is the current directory.
An example for this prompt is: uira@notebook:home$
Another important Shell variable is PATH. The path stores a list of directories that contain programs that you can execute without needing to pass the full path of their location on the command line.
$ echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin
It's important that you know that the bash command interpreter follows this order to find and execute typed commands:
- Is the typed command an internal command of the command interpreter?
- If not, is the command an executable program located in some directory listed in the PATH variable?
- Was the command's location explicitly declared?
An interesting tip is to include ":." at the end of the PATH variable so that bash includes the current directory in its search list for executable programs.
Unlike MS-DOS, the current directory is not included in the default search list. Without including ":." in PATH, it's necessary to inform the relative path when calling programs in the current directory with "./programname".
This is a problem for novice Linux users who don't understand why typing a command or trying to run a program that is in the current folder doesn't work most of the time. It's simply the lack that ":." makes in $PATH.
A complete list of shell variables can be obtained with the command:
$ set
You can also change or create a new shell variable with the command:
$ LIVRO=”Certificação Linux”
Preferably, variables should be declared in uppercase and don't put spaces between the variable name, the = symbol and its content.
If the content is alphanumeric, it's desirable that it be enclosed in single or double quotes.
When the text of a variable is a common sequence of letters and numbers, it doesn't matter much if you use single or double quotes.
$ FRASE1=”Este é um teste”
$ FRASE2='de direfença entre aspas'
$ echo $FRASE1 $FRASE2
Este é um teste de diferença entre aspas
But if you're going to use variables within quotes, there is a difference:
$ echo “$FRASE1 $FRASE2”
Este é um teste de diferença entre aspas
$ echo '$FRASE1 $FRASE2'
$FRASE1 $FRASE2
So double quotes expand the contents of variables, while single quotes don't. This matters if you want, for example, to include a directory in the PATH variable:
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin
$ PATH="$PATH:/ora/oracle/admin/bin"
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin:/ora/oracle/admin/bin
In this case single quotes would not work.
After creating a shell variable, you need to export it to the environment with the export command.
When a variable is exported to the environment, it becomes available to all child processes of the shell (all programs and applications you run in bash).
Each time a program is executed by the shell, it will only receive variables created by the shell if they are exported with the export command.
Thus the child process (the program we want to execute) will inherit from the parent process (the shell) the created variables.
See the example below, where we create a variable called LIVRO:
$ LIVRO=”Certificação Linux”
$ echo $LIVRO
Certificação Linux
If we run bash again in the same terminal, to create a child process, you'll see that the LIVRO variable doesn't exist, because it wasn't exported to child processes:
$ bash
$ echo $LIVRO
Using Wildcards
When we are working with files and directories in the shell, it's very common to need to work with multiple files at once. To make this type of task simpler, Linux offers the wildcard feature for file and directory names.
Just like in a deck of cards, wildcards are symbols that can mean many things. In Linux, they allow you to use them as part of file and directory names. See the table below:
| Symbol | Description |
| * | Means "matches anything" and can replace one or more characters of a name. For example "Certifi*" can mean "Certificado", "Certificação" or any other combination of filenames starting with "Certifi". |
| ? | Means it can replace only one character of a name. For example: "?ertificado" can replace "Certificado", "certificado" or any other combination of filenames starting with any character and ending with "ertificado". |
| {text1, text2...} | Replaces the part inside the braces with text1, then text2, and so on. For example: "part_{a,b,c}" will result in "part_a", "part_b" and "part_c". |
Examples:
$ ls *.txt
Lists all files with the .txt suffix
$ cat doc??? >> documentos
Concatenates all files starting with "doc" that have 6 characters in size into the documentos file.
Let's look at the commands to list typed commands, export variables and check if something is a command.
If you want to know how to copy files in Linux, take a look at this post.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.
