Introduction
This tutorial, the first in a series that teaches Linux basics, covers getting started with the terminal, the Linux command line, and running commands. If you're new to Linux, you should familiarize yourself with the terminal, as it's the standard way to interact with a Linux server.
If you want to get the most out of this tutorial, you'll need a Linux server to connect to and use. If you don't already have one, you can quickly spin one up by following this link: How to Create a Digital Ocean Droplet. This tutorial was written for Ubuntu 22.04 Server, but the general principles apply to any other Linux distribution.
Let's start by explaining what a terminal emulator is.
Terminal emulator
A terminal emulator is a program that allows you to use a terminal in a graphical environment. Since most people use an operating system with a graphical user interface (GUI) for their everyday computing needs, using a terminal emulator is a necessity for most Linux server users.
Here are some of the most popular free terminal emulators by operating system:
- Macos: Terminal (default), iTerm2
- Windows: ConEmu, Windows Terminal, PuTTy
- Linux: Gnome Terminal, Konsole, XTerm
Each terminal emulator has its own set of features. In general, you should expect a modern terminal emulator to support tabbed windows and text highlighting.
Shell
On a Linux system, a shell is a command-line interface that interprets user commands and script files and tells the server operating system what to do with them. There are several shells that are widely used, such as the Bourne-Again shell (bash) and the Z shell (zsh). Each shell has its own set of features and complexities related to how commands are interpreted, but they all have input and output redirection, variables, condition testing, and more.
This tutorial is written using the Bourne-Again shell, commonly called bash, which is the default shell of most Linux distributions, including Ubuntu, Fedora, and RHEL.
Command line
When you first log into a server, you are usually greeted with a message of the day (MOTD), which is usually an informational message that includes miscellaneous information such as the version of Linux distribution the server is running. After the MOTD, you are taken to a command line or shell prompt, where you can issue commands to the server.
The information provided on the command line can be customized by the user, but here is an example of the default Ubuntu 20.04 command line:
sammy@webapp:~$
Here is a summary of the command line syntax:
- sammy: username of the current user
- webapp: server hostname
- ~: The current directory. In bash, the default shell, ~ or tilde is a special character that expands to the path to the current user's home directory. In this case, it represents /home/sammy
- $: Notification icon. This indicates the end of the command line, after which user keyboard input appears.
Here is an example of what the command line might look like if entered as root and in the /var/log folder:
root@webapp:/var/log#
Note that the symbol that ends the command line is a #, which is the standard line symbol for root. In Linux, the root user is a superuser account, which is a special user account that can perform system-wide administrative functions. It is an unrestricted user with permission to do anything on the server.
Running commands
Commands can be issued at the command line by specifying the name of an executable file, which can be a binary program or a script. There are many standard Linux commands and tools that come installed with the operating system that allow you to navigate the file system, install software packages, and configure the system and applications.
An instance of a running command is known as a process. When a command is executed in the foreground, which is the default way of executing commands, the user must wait for the process to finish before returning to the command prompt, at which point they can continue issuing more commands.
It's important to note that almost everything in Linux is case-sensitive, including file and directory names, commands, arguments, and options. If something doesn't work as expected, double-check the spelling and capitalization of your commands!
Here are some examples that cover the basics of executing commands.
Without arguments or options.
To run a command without arguments or options, type the command name and press Enter.
If you run a command like this, it will display its default behavior, which varies from command to command. For example, if you run the cd command without any arguments, you will be returned to your current user's home directory. The ls command prints a list of files and directories in the current directory. The ip command without any arguments prints a message that shows you how to use the ip command.
To list the files and directories in your current directory, run the ls command without arguments (there may be none):
lsBy argument
Many commands accept arguments, or parameters, that can affect the behavior of a command. For example, the most common way to use the cd command is to pass a single argument that specifies which directory to change to. For example, to change to the /usr/bin directory, where many of the standard commands are installed, issue this command:
cd /usr/bin
The cd part is the command and the first argument /usr/bin follows the command. Notice how your current command line path has been updated.
Try running the ls command to see the files that are in your current new directory.
ls
Output
…
grub-mkrescue sdiff zgrep
grub-mkstandalone sed zipdetails
grub-mount see zless
grub-ntldr-img select-editor zmore
grub-render-label semver znew
grub-script-check sensible-browserWith options
Most commands accept options, also known as flags or switches, that modify the behavior of the command. Options follow a command and are represented by a single character followed by one or more options, represented in uppercase or lowercase letters. Some multi-word options can begin with — followed by the flag text.
For an example of how options work, let's look at the ls command. Here are some common options that are useful when using ls:
- -l: Print a “long list”, which includes additional details such as permissions, ownership, file size, and timestamp.
- -a: List all files in the directory, including hidden files (those starting with .)
To use the -l flag with ls, use this command:
ls -lNote that the list contains the same files as before, but with additional information about each file.
As mentioned earlier, options can often be grouped together. If you want to use the -l and -a options together, you can run ls -l -a or just combine them like this:
ls -laNote that the list includes hidden . and .. directories in the list, due to the -a option.
With options and arguments
It is almost always possible to combine options and arguments when executing commands.
For example, you can examine the contents of /home without paying attention to your current directory by running the ls command:
ls -la /home
ls is the command, -la is the option, and /home is the argument that indicates which file or directory to list. This should print a detailed listing of the /home directory, which should include the home directories of all regular users on the server.
Environmental variables
Environment variables are named values that are used to change how commands and processes are executed. When you first log in to a server, several environment variables are set by default based on several configuration files.
View all environment variables
To view all the environment variables set for a particular terminal session, run the env command:
env
It will probably have a lot of output. Look for the PATH entry:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/gamesThe PATH environment variable is a list of directories, separated by colons, in which the shell looks for executable programs or scripts when issuing a command. For example, the env command is located in /usr/bin, and you can run it without specifying the full path because its path is in the PATH environment variable.
View the value of a variable
The value of an environment variable can be retrieved by prefixing the variable name with a $. This expands the referenced variable to its value.
For example, to print the value of the PATH variable, you can use the echo command:
echo $PATH
Or you can use the HOME variable, which is set to the user's home directory by default, to change to your home directory:
cd $HOME
If you try to access an environment variable that is not set, you will expand it to nothing. An empty string.
Setting environment variables
Now that you know how to view your environment variables, you need to learn how to set them.
To set an environment variable, all you need to do is start with the variable name, immediately followed by an = sign, and then its desired value:
VAR=value
Note that if you set an existing variable, the original value will be overwritten. If the variable did not exist to begin with, it will be created.
Bash includes a command called export that exports a variable to be inherited by child processes. This allows you to use scripts that reference an environment variable exported from your current session.
You can also reference existing variables when setting a variable. For example, if you installed a program in /opt/app/bin, you can add that directory to the end of your PATH environment variable with this command:
export PATH=$PATH:/opt/app/bin
Now check that /opt/app/bin is added to the end of your PATH variable with echo:
echo $PATH
Keep in mind that setting environment variables this way only sets them for your current session. This means that if you log out or switch to another session, the changes you made to the environment will not be preserved. There is a way to permanently change environment variables, but that will be covered in a later tutorial.
Result
Now that you've started learning about the Linux terminal (and a few commands), you should have a good foundation for expanding your knowledge of Linux commands. Read the next tutorial in this series to learn how to navigate, view, and edit files and their permissions.









