Categories: How-To

Linux Shell for Dummies [Basic Guide]

It is important to say that even with a beautiful, practical and diverse graphical environment, the strength of Linux lies on the command line. For advanced users, it’s in the operating system shell that the magic happens, because Linux allows the user to do whatever they want, with the ease of changing a file. The concatenation of commands also allows you to create true computational Swiss Army knives.

Access to the terminal is simple and fast. Just search for the Terminal application in the graphical environment, or press the Ctrl-Alt-F1… F2… F3.. key sequence to access the terminal.

Linux Shell for Dummies: The Command Line

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 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 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

Or

$ ls —lta

In the case of the ls command, the options can be passed 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.

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.

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

Or

$ /bin/ls

During the exam it is very important that you are well familiar with the commands listed in this chapter and their syntax. So 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 entered commands is fabulous. This can be very useful for auditing purposes, remembering 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 “~” symbol refers to the home directory of the logged in user.

Shell Variables

The user’s first contact with the Linux shell is the command prompt.

How this command prompt appears may change with each distribution, from the simplest, with a simple “$” or with user information, server name, and even the current directory:

[laugh @server1 ~] $

The command prompt can be customized by configuring a variable that the Linux shell maintains called PS1.

The variables are loaded at the start of the bash execution and can also be set manually at any time.

The first variable that we are going to address is 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 incoming commands, but they can be of great help when loading extra information such as user name, current directory, etc.

It was agreed that the variables are all written in an upper-case format. But it’s important to 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 succeeds it must be interpreted exactly as it is and not processed by the shell. It is what we call a metacharacter.

It is common for the standard bash command prompt to come in the form: 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 user’s name, 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 the directories containing the programs that you can run without having to pass the full path to your location on the command line.

$echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin

It’s important to know that the bash command interpreter follows the following order to find and execute the entered commands:

  1. Is the command entered an internal command of the command interpreter?
  2. If not, is the command an executable program located in a directory listed in the PATH variable?
  3. Was the location of the command explicitly stated?

An interesting tip is to include “:. ” at the end of the PATH variable for bash to include the current directory in its search list for executable programs.

Unlike MS-DOS, the current directory is not included in the standard search list. Without including the “:.” in the PATH it is necessary to enter the relative path when calling programs in the current directory with”. /program name”.

This is a problem for new Linux users who don’t understand because 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 of “:.” 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 using the command:

$ BOOK=” Linux Certification”

Preferably, variables should be declared in an upper box and do not place spaces between the variable name, the = symbol and its content.

If the content is alphanumeric, it is desirable that it be surrounded by single or double quotes.

When the text of a variable is a common sequence of letters and numbers, it doesn’t matter if you’re going to use single or double quotes.

$ PHRASE1=” This is a test”
$ frase2='Of the difference between quotation marks'
$ echo $PHRASE1 $PHRASE2
This is a quotation mark difference test

But if you are going to use variables in quotes, there is a difference:

$echo “$PHRASE1 $PHRASE2”
This is a quotation mark difference test

$ echo '$PHRASE1 $PHRASE2' $PHRASE1 $PHRASE2

So double quotes expand the content of the variables, while single quotes don’t. This makes a difference 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: /oracle/admin/bin”
$echo $PATH /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin:/oracle/admin/bin

In this case single quotes wouldn’t work.

After creating a shell variable, you must export it to the environment with the export command.

When a variable is exported to the environment, it is available to all shell child processes (all programs and applications that 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.

So the child process (the program that we want to execute) will inherit the variables created from the parent process (the shell).

See the example below, where we created a variable called BOOK:

$ BOOK=” Linux Certification”
$ echo $BOOK
Linux Certification

If we run bash again in the same terminal to create a child process, you will see that the BOOK variable does not exist because it was not exported to the child processes:

$bash
$ echo $BOOK

Use of Jokers

When we are working with files and directories in the shell, it is very common to have to work with several files at once. To make this type of task simpler, Linux offers the wild card feature for file and directory names.

Just like in the deck of cards, jokers are symbols that can mean many things. On Linux, they allow you to use them as part of the name of files and directories. See the table below:

It

Symbol Description
* means “anything goes” and can replace one or more characters in a name. For example, “Certifi*” can mean “Certificate”, “Certification”, or any other combination of file names starting with “Certifi”.
? It means that you can replace a character only in a name. For example: “? Certificate” can replace “Certificate”, “certificate”, or any other combination of file names starting with any character and ending with “certificate”.
{text1, text2…} Replace 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 suffix .txt

$ cat doc??? >> documents

Concatenate all files starting with “doc” that are 6 characters long in the documents file.

Let’s look at the commands to list the commands entered, export variables, and verify that something is a command.

If you want to know how to copy files on Linux, take a look at this post.

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.