Getting started on the command line

0

Image by: Opensource.com CC-by-SA 4.0

As a SysAdmin, the Linux command line is typically my happy place. The command line interface, the CLI, is the closest I can get to the raw power of my computers. It gives me access to tools that make my work easy, fast, and even fun. I use the command line so much that I sometimes forget how much power I have to manage my computers.

I also forget that it took me a long time to really understand the terminology that applies to the various tools that I use to access that command line. This article is about those tools and the sometimes confusing terminology necessary to function on the CLI.

CLI terminology

There are several terms relating to the command line that are often used interchangeably. This indiscriminate usage of the terms caused me a good bit of confusion when I first started working with Unix and Linux. I think it is important for SysAdmins, and more casual users as well, to understand the differences between the terms console, virtual console, terminal, terminal emulator, a terminal session, and shell.

Of course you can use whatever terminology works for you so long as you get your point across. I typically try to be as precise as possible because the reality is that there are significant differences in the meanings of these terms and it sometimes matters.

Command prompt

The command prompt is a string of characters like this one that sits there with a cursor which may be flashing, and waiting – prompting – you to enter a command.

tuser1@testvm1:~$ █

The typical command prompt in a modern Linux installation consists of the user name, the host name, and the present working directory (PWD), also known as the “current” directory, all enclosed in square braces. The tilde (~) character is Linux shorthand for the home directory.

Command line

The command line is the line on the terminal that contains the command prompts and any command you enter.

All of the modern mainstream Linux distributions provide at least three ways to access the command line. If you use a graphical desktop most distributions come with multiple terminal emulators from which to choose. The graphical terminal emulators run in a window on the GUI desktop and more than one terminal emulator can be open at a time.

Linux also provides the capability for multiple virtual consoles to allow for multiple logins from a single keyboard and monitor (KVM). Virtual consoles can be used on systems that don’t have a GUI desktop but they can be used even on system that do have a GUI.

The last method to access the command line on a Linux computer is via a remote login. Telnet was a common tool for remote access for many years, but because of greatly increased security concerns it has largely been replaced by Secure Shell (SSH). SSH is implemented in Linux by openSSH.

Command-Line Interface

The command-line interface (CLI) is any text mode user interface to the Linux operating system that allows the user to type commands and see the results as text output.

Command

Commands are what you type on the command line in order to tell Linux what you want it to do for you. Commands have a general syntax that is easy to understand. The basic command syntax for most shells is:

command [-o(ptions)] [arg1] [arg2] ... [argX]

Options may also be called switches. They are usually a single character and are binary in meaning; that is, to turn on a feature of the command, such as using the -l option in ls -l to show a long listing of the directory contents. Arguments are usually text or numerical data that the command needs to have in order to function or produce the desired results. For example, the name of a file, directory, user name, and so on, would be an argument. Many of the commands that you will discover in your Linux journey use one or more options and sometimes an argument.

If you run a command that simply returns to the CLI command prompt without printing any additional data to the terminal don’t worry, that is what is supposed to happen with most commands. If a Linux command works as it is supposed to without any errors, most of the time it will not display any result at all. Only if there is an error will any message display. This is in line with that part of the Linux Philosophy – and there is a significant discussion about that which I won’t cover here – that says, “Silence is golden.”

Command names are also usually very short. This is called the “Lazy Admin” part of the Linux Philosophy; less typing is better. The command names also usually have some literal relation to their function. Thus the “ls” command means “list” the directory contents, “cd” means change directory, and so on.

Note that Linux commands are case sensitive and are lowercase. Commands will not work if entered in upper case. For example, ls will work but LS will not. File and directory names are also case sensitive. Of course, just to make things more complicated for myself, I have written a few scripts that use an uppercase character or two in their names.

Terminal

The original meaning of the word “terminal” in the context of computers is an old bit of hardware that provides a means of interacting with a mainframe or Unix computer host. In this article the term will refer to terminal emulator software that performs the same function.

Figure 1: A DEC VT100 dumb terminal. This file is licensed under the Creative Commons Attribution 2.0 Generic license. Author: Jason Scott

The terminal is not the computer; the terminals merely connect to mainframes and Unix systems. Terminals – the hardware kind – are usually connected to their host computer through a long serial cable. Terminals such as the DEC VT100 shown in Figure 1 are usually called “dumb terminals” to differentiate them from a PC or other small computer that may act as a terminal when connecting to a mainframe or Unix host. Dumb terminals have just enough logic in them to display data from the host and to transfer keystrokes back to the host. All of the processing and computing is performed on the host to which the terminal is connected.

Terminals that are even older, such as mechanical teletype machines (TTY) predate the common use of CRT displays. They used rolls of newsprint-quality paper to provide a record of both the input and results of commands. The first college course I took on computer programming in BASIC used these TTY devices which were connected by telephone line at 300 bits per second to a GE (yes, General Electric) time-sharing computer a couple hundred miles away.

Much of the terminology pertaining to the command line is rooted by historical usage in these dumb terminals of both types. For example the term TTY is still in common use but I have not seen an actual TTY device in a many years. Look in the /dev directory of your Linux or Unix computer and you will find a large number of TTY device files.

tuser1@testvm1:~$ ls /dev | grep tty | column 
tty     tty17   tty26   tty35   tty44   tty53   tty62   ttyS13  ttyS22  ttyS31
tty0    tty18   tty27   tty36   tty45   tty54   tty63   ttyS14  ttyS23  ttyS4
tty1    tty19   tty28   tty37   tty46   tty55   tty7    ttyS15  ttyS24  ttyS5
tty10   tty2    tty29   tty38   tty47   tty56   tty8    ttyS16  ttyS25  ttyS6
tty11   tty20   tty3    tty39   tty48   tty57   tty9    ttyS17  ttyS26  ttyS7
tty12   tty21   tty30   tty4    tty49   tty58   ttyS0   ttyS18  ttyS27  ttyS8
tty13   tty22   tty31   tty40   tty5    tty59   ttyS1   ttyS19  ttyS28  ttyS9
tty14   tty23   tty32   tty41   tty50   tty6    ttyS10  ttyS2   ttyS29
tty15   tty24   tty33   tty42   tty51   tty60   ttyS11  ttyS20  ttyS3
tty16   tty25   tty34   tty43   tty52   tty61   ttyS12  ttyS21  ttyS30
tuser1@testvm1:~$

Terminals were designed with the singular purpose of allowing users to interact with the computer to which they were attached by typing commands and viewing the results on the roll of paper or the screen. The term, “terminal,” tends to imply a hardware device that is separate from the computer while being used to communicate and interact with it.

Console

A console is a special terminal because it is the primary terminal connected to a host. It is the terminal at which the system operator would sit to enter commands and perform tasks that were not allowed at other terminals connected the host. The console is also the only terminal on which the host would display system-level error messages when problems occurred.

Figure 2. Unix developers Ken Thompson and Dennis Ritchie. Thompson is sitting at a teletype terminal used as a console to interface with a DEC computer running Unix. Peter Hamer – Uploaded by Magnus Manske.

Figure 2 shows Unix developers Ken Thompson and Dennis Ritchie at a DEC computer running Unix. Thompson is sitting at a teletype terminal used as a console to interface with the computer.

There can be many terminals connected to mainframe and Unix hosts, but only one can act as a console. On most mainframes and Unix hosts, the console was connected through a dedicated connection that was designated specifically for the console. Like Unix, Linux has runlevels and some of the runlevels such as run level 1, single user mode, and recovery mode are used only for maintenance. In these runlevels only the console is functional to allow the SysAdmin to interact with the system and perform maintenance.

On a PC the physical console is usually the keyboard, monitor (video), and sometimes the mouse (KVM1) that are directly attached to the computer. These are the physical devices used to interact with BIOS during the BIOS boot sequence, and can be used during the early stages of the Linux boot process to interact with GRUB and choose a different kernel to boot or modify the boot command to boot into a different run level.

Because of the close physical connection to the computer of the KVM devices, the SysAdmin must be physically present at this console during the boot process in order to interact with the computer. Remote access is not available to the SysAdmin during the boot process and only becomes available when the SSHD service is up and running.

Virtual consoles

Modern personal computers and servers that run Linux do not usually have dumb terminals that can be used as a console. Linux typically provides the capability for multiple virtual consoles to allow for multiple logins from a single, standard PC keyboard and monitor. Red Hat Enterprise Linux, CentOS and Fedora Linux usually provide for six or seven virtual consoles for text mode logins. If a graphical interface is used, the first Virtual console, vc1, becomes the first graphical (GUI) session after the X Window System (X) starts. and vc7 becomes the second GUI session. Figure 3 shows the terminal session in virtual console 2.

Figure 3. Login prompt for virtual console 2.

Each virtual console is assigned to a function key corresponding to the console number. So vc1 would be assigned to function key F1, and so on. It is easy to switch to and from these sessions. On a physical computer you can hold down the Ctrl-Alt keys and press F2 to switch to vc2. Then hold down the Ctrl-Alt keys and press F1 to switch to vc1 and what is usually the graphical desktop interface.

Virtual consoles provide a means to access multiple consoles using a single physical system console, the keyboard, video display and mouse (KVM). This gives administrators more flexibility to perform system maintenance and problem solving. There are some other means for additional flexibility, but Virtual Consoles is always available if you have physical access to the system or directly attached KVM device or some logical KVM extension such as Integrated Lights Out (ILO). Other means such as the screen command might not be available in some environments and a GUI desktop will probably not be available on most servers.

The reset command resets all terminal settings. This is useful if the terminal becomes unusable or unreadable, such as after cat’ing a binary file. Even if you cannot read the reset command as you input it, it will still work. I have on occasion had to use the reset command twice in a row.

The virtual consoles are assigned to device files such as /dev/tty2 for virtual console 2 as in Figure 3. The Linux Console is the terminal emulator for the Linux virtual consoles.

Terminal emulator

A terminal emulator is a software program that emulates a hardware terminal. Most of the current graphical terminal emulators, like the Xfce4 terminal emulator seen in Figure 4, can emulate several different types of hardware terminals. Most terminal emulators are graphical programs that run on any Linux graphical desktop environment like Xfce, KDE, Cinnamon, LXDE, GNOME, and others.

Figure 4. The Xfce4 terminal emulator with two tabs open.

You can see in Figure 4 that a right-click on the Xfce4 terminal emulator window brings up a menu that allows opening another tab or another emulator window. This figure also shows that there are currently two tabs open. You can see them just under the icon bar.

The first terminal emulator was Xterm which was originally developed in 1984 by Thomas Dickey. The original Xterm is still maintained and is packaged as part of many modern Linux distributions. Other terminal emulators include Xfce4-terminal, GNOME-terminal, Tilix, rxvt-unicode, Terminator, Konsole, and many more. Each of these terminal emulators has a set of interesting features that appeal to specific groups of users. Some have the capability to open multiple tabs or terminals in a single window. Others provide just the minimum set of features required to perform their function and are typically used when small size and efficiency are called for.

My favorite terminal emulators are Xfce4-terminal, Konsole, and Tilix because they offer the ability to have many terminal emulator sessions in a single window. The Xfce4-terminal, Konsole, and terminal do this using multiple tabs that I can switch between. Tilix and Konsole offer the ability to tile multiple emulator sessions in a window session as well as providing multiple sessions.

Figure 5: An instance of Konsole with three tabs and three panels open in the visible tab.

My current terminal emulator of choice is Konsole, shown in Figure 5, the default terminal emulator for the KDE Plasma desktop. I like it because it offers a good feature set and it’s also very lightweight and uses few system resources. Other terminal emulator software provides many of these features but not as adroitly and seamlessly as Konsole, Xfce4, terminal, and Tilix.

Yes, I know there are other terminal emulators–many of them–one of which might already be your favorite.

Pseudo Terminal

A pseudo terminal is a Linux device file to which a terminal emulator is attached in order to interface with the operating system. The device files for pseudo terminals are located in the /dev/pts directory and are created only when a new terminal emulator session is launched. That can be a new terminal emulator window or a new tab or panel in an existing window of one of the terminal emulators, such as terminal, that supports multiple sessions in a single window.

The device files in /dev/pts are simply a number for each emulator session that is opened. The first emulator would be /dev/pts/1, for example.

Session

Session is another of those terms that can apply to different things while retaining essentially the same meaning. The most basic application of the term is a to terminal session. That is a single terminal emulator connected to a single user login and shell. So in its most basic sense a session is a single window or virtual console logged into a local or remote host with a command line shell running in it. The Xfce4 terminal emulator supports multiple sessions by placing each session in a separate tab.

Shell

A shell is the command interpreter for for the operating system. Each of the many shells available for Linux interprets the commands typed by the user or SysAdmin into a form usable by the operating system. When the results are returned to the shell program, it displays them on the terminal. Any Linux shell can run in any terminal emulator.

The default shell for most Linux distributions is the bash shell. bash stands for Bourne Again Shell because the bash shell is based upon the older Bourne shell which was written by Steven Bourne in 1977. Many other shells are available. The four I list here are the ones I encounter most frequently but many others exist12.

  • csh: The C shell for programmers who like the syntax of the C language.
  • ksh: The Korn shell, written by David Korn and popular with Unix users.
  • tcsh: A version of csh with more ease-of-use features.
  • zsh: Which combines many features of other popular shells.

All shells have some built-in commands that supplement or replace the commands provided by the core utilities. Open the man page for bash and find the “BUILT-INS” section to see the list of commands provided by the shell itself.

I have used the C shell, the Korn shell and the Z shell. I still like the Bash shell better than any of the others I have tried. Each shell has its own personality and syntax. Some will work better for you and others not so well. Use the one that works best for you, but that might require that you at least try some of the others. You can change shells quite easily.

Summary

This article introduced you to the terminal emulators and shells that allow you to access the command line. There are many emulators all of which have features to recommend them to users. I’ve tried many different terminal emulators and I like several of them. My current favorite changes from time to time and depends upon my needs. Konsole is my current favorite but I also use Xfce4-terminal.

Each distribution has its own default terminal emulator, all of which perform the basic task of allowing user access to the command line. Because there are so many good ones, I suggest you try as many as you can and choose the one that’s best for you and the way you work.


  1. KVM stands for Keyboard, Video, and Mouse, the three devices that most people use to interact with their computers. ↩︎

Leave a Reply