A gentle (yet complete) introduction to Linux shell and terminal
Many newbie Linux users are intimated by the word “terminal” or even worse “shell” fearing that someday they too will be forced to adventure in this hostile world. In this article I’ll try to demystify the “fear of the terminal” by explaining everything a beginner should know *insert roll of drums* from the beginning. By the end of this article you will be able to tell the difference between shell and terminal, know how to access them, and move your way around!
What’s a shell/terminal? What’s the difference? (Bonus: tty?)
Let’s make things clear once and for all: a shell and a terminal are two different things, but they interact on a regular basis.
A shell is an interactive program that interacts with the user, and allows the user to interact with the system. They are also sometimes called command-line interpreters. Shells allow user to execute commands (with or without inputs) and retrieve results (output). Modern shells also allow scripting, a basic form of computer programming that allows basic (sometimes complex) operations. Programs that fall under this category are known as shell scripts and usually end with the .sh extension. If you’re interested in shells history you can find more in this article by IBM.
A terminal (abbreviation of terminal emulator) is a graphical software that allows the user to open a shell within a graphical user interface (GUI). On top of that a terminal emulator makes operations such copy and paste easy, whereas it would be difficult to perform them within the shell itself.
There is also another actor in this play: the tty. The acronym stands for TeleTYpe an interface for the homonym device used in the past. Teletypes used to work as input/output systems that allowed users to interact with the system, long before monitors and graphical user interfaces became mainstream. Although physical teletypes are now rare, the software emulation lives on in Linux operating systems. You can access a tty using the combination ctrl+alt+f2 (or any other f-key, so long the operating system supports it, usually stopping around 8). By going into a tty you are presented with a shell. To return to your graphical environment go to tty1 or 7 (depending on the distribution). If you want to know more about ttys here’s an excellent article.
How to open a terminal
Opening a Linux terminal isn’t really a difficult task. Whatever your Linux operating system is, whatever your desktop environment is, you can simply search for the word “terminal” in your search bar, or browse through the home menu and find “terminal” or “konsole”.
What am I looking at?
In the picture above you’re seeing a terminal emulator in action, within that terminal you can see a shell. Within the shell you’re looking at the prompt ([email protected]:~$). Conventionally, prompts follow this format (but there’s no strict enforcement):
- the part before the @ (at sign) is the username of the logged in user;
- the part after the @ (at sign) and before the : (column) is the hostname of the machine;
- the part after the : (column) and before the $ (dollar sign) or # (hashtag) is the current directory;
You might’ve noticed that I mentioned the hashtag (#) and the dollar sign ($) as two different “prompt-enders”. The dollar sign conventionally tells the user is a normal user, while the hashtag is used when the user is root (a special privileged user). It is also not uncommon for online tutorial to indicate whether a command should be executed as a normal user using the dollar sign and as root user using the hashtag.
After the end of the prompt is the cursor: here you can write your commands, try typing “whoami” (without quotes) and press enter, that way you’ll have issued the first command inside your terminal!
Commands within *nix systems, hence also Linux, are programs or shell scripts. These commands (programs) can also take arguments (input) and flags (sometimes called options or switches): arguments that modify the command behavior that may or may not accept input. Let’s make an example:
$ command -h --help --output=value -o value -ovalue -o=value argument6 argument7
In this example you can see the command with many flags, options, parameters and arguments. Firstly let’s understand arguments: each line you issue in the shell is divided in many arguments, the first one (argument 0) is usually the command itself, in this case “command“. Then there are two flags: -h and –help, usually these are the same, but you can notice two different formats: the first one is in the so-called Unix-style while the second one is called GNU-style. After that there are three more “-o” flags, each represent a valid format that may or may not be valid for that particular command, you should always consult the command help (using -h or –help) or the manual page (using man command). Last two arguments are normal arguments that may or may not be required.
What is sudo? Is it used to execute commands?
Sudo is a special and ubiquitous command. Sudo stands for “Super User DO” it is used by normal users to achieve elevated privileges, whatever command is written after sudo will be executed as if the user was privileged, provided he has access to privileges. No, sudo isn’t used to execute commands. For the expert people out there, you’d be surprised how many times I’ve been told/asked so.
Where to go from here?
I have written a number of articles that can help you understand better what you can do with the terminal, also almost every tutorial article in this site uses the terminal. Here’s a few resources:
- An introductory guide to Linux CLI
- Linux permissions (basic)
- Special permissions: sticky bit, suid and sgid
- Linux permissions: chown, chgrp and chmod
- Linux users and groups the complete guide