All about the shell - Linux Nitty Gritty: Working at the Ubuntu Command-Line Prompt (2011)

Linux Nitty Gritty: Working at the Ubuntu Command-Line Prompt (2011)

All about the shell

When we talk about the “command-line”, we’re talking about issuing typed commands directly to Linux. Most commands relate to manipulating files, while some administer the system. The command-line offers power and flexibility, at the expense of a slightly steep learning curve and—arguably—a lack of intuitiveness.

bashed about

The command-line utilized in Ubuntu is known as bash—the Bourne Again SHell. This is an evolved version of the Bourne sh program, one of the oldest command-line programs for Unix. Most people agree that bash offers the best all-round mix of functionality and ease-of-use.

Command-line programs are sometimes known as shells, and the term comes from the fact that—like mollusks and crustaceans—the shell “wraps around” the delicate interior of the operating system, protecting it from accidental damage!

NOTE A graphical user interface is sometimes referred to as a shell because, by the above definition, it has the same function as a command-line prompt.

Other shell programs are sometimes used under Linux instead of bash. Perhaps the most popular are Korn Shell (ksh) and C Shell (csh). Both are geared towards programming and it’s unlikely you’ll ever come into contact with them. bash is the default in most popular Linux distros.

To DOS or not to DOS

You might be wondering if the Linux command-line is similar to Microsoft DOS. They’re distant cousins rather than siblings. DOS was a clone of CP/M, that itself borrowed much from the Unix command-line. Some DOS knowledge will give you a head start, but you will have to unlearn as much as you learn!

Understanding the prompt

Let’s get stuck-in straight away.

Starting a command-line session

There are two ways to start a command-line session: by running a desktop terminal program (sometimes known as a terminal emulator), or by switching to a virtual console (also known as a virtual terminal). In both cases you’re accessing exactly the same command-line.

To switch to a virtual console, hit Ctrl+Alt+F2. The GUI will disappear and be replaced by a login prompt. Don’t worry—your desktop is still there, and you can switch back to it by hitting Ctrl+Alt+F7. It’s just that the virtual console needs to take over the screen.

NOTE There are six virtual consoles, and they’re accessed by hitting Ctrl+Alt and F1, F2, F3, F4, F5 or F6. The console on F1 is used for debug and log output, so is best avoided.

Login by typing your username, and then the password. You won’t be prevented from logging in because you’re already logged in at the desktop—under Linux a user can login as many times as she wants.

As you might be realizing, a virtual console session is a little clunky. A more convenient way to access the command-line is to use a terminal program. This provides a command-line right there on the desktop.

Logout of the virtual console by typing exit, and switch back to your desktop (hit Ctrl+Alt+F7). Then open a terminal window by clicking Applications > Accessories > Terminal.

This time there’s no need to login, because the terminal window runs as part of your desktop environment, and that’s already logged-in.

NOTE So why use a virtual console? Well, they’re very useful when things go wrong. If the GUI crashes, you can switch to a virtual console to try and fix things. Even if there’s no GUI subsystem, the virtual console will still be there. It’s a permanent fixture of Linux.

Knowing who you are

When the terminal program appears, you’ll see something like this:

keir@keir-desktop:~$

This is the actual command-line prompt, often shorted to “prompt”.

The first part of the prompt shows your username. In this example, taken from my test PC, the user is keir. After the @ sign is the name of the computer, commonly referred to as the hostname. This was set during installation of Ubuntu, on the same configuration screen where you chose your username.

The hostname is how the computer is known on the network. It isn’t very important if your computer only connects to the Internet via a router or modem, but it’s vital if Ubuntu is used in a server environment, or if you intend to remotely access it across the Internet.

As you can see, the computer in my test PC setup is called keir-desktop and so, reading the full prompt, we can see that the user named keir is logged in at (@) the computer named keir-desktop. In other words, the first part of the prompt is all about who you are and where you’re logged in.

Knowing where you’re browsing

After this is a colon. This separates the physical location part of the prompt from the rest, that tells us the location in the filesystem—which folder we’re currently browsing. We see a ~ symbol (known as a tilde). This is shorthand that always indicates the user’s /home folder. When you see a tilde, imagine the path to your home folder instead.

TIP You can also use the tilde yourself when typing commands to save typing out the entire path to your /home folder.

So, reading from left to right, the prompt tells us that the user keir, logged in at the computer called keir-desktop, is currently browsing his /home folder—in this case, that’s /home/keir.

The final component in the line is a dollar sign. This indicates that you’re logged in as an ordinary user. If you ever log in directly as root (discussed in the Working with Root Powers section later), the prompt changes to a pound sign (#; also called a hash sign).

Try switching to your Documents folder by typing the following (hit the Enter key when you’ve finished typing the line):

cd Documents

NOTE Remember: upper and lower case letters matter in Ubuntu! You must type Documents and not documents or DOCUMENTS!

The prompt will change to something like the following:

keir@keir-desktop:~/Documents$

Once again, from left to right, the prompt says that the user keir on the computer called keir-desktop is browsing the Documents folder in his /home folder (i.e. /home/keir/Documents).