Read a sample: 1. Meet the Shell and the Command
A Linux command line can look deceptively simple: a prompt, a few words, and perhaps a result. Behind that small exchange are several distinct jobs. A shell reads what you type, interprets it, and carries out the requested action. Sometimes it handles the action itself; sometimes it starts another program. The program may receive information, produce a result, or report a problem.
Understanding those jobs makes commands easier to combine and troubleshoot. Before connecting commands with pipes, you need a comfortable place to practice and a clear picture of what happens when you press Enter.
In this chapter, you will create a practice directory, recognize the shell prompt, and run commands with arguments. You will also compare a shell built-in with an external program—a distinction that will help explain where commands come from and why they behave differently.
On a Linux desktop, open an application named Terminal, Console, or something similar. If you are using a remote Linux system, connect using the method provided for that system. Either way, you are looking for a text interface in which you can enter commands.
The terminal and the shell are related but different:
- The terminal provides the interface where you type and see text.
- The shell reads your command lines and interprets them.
- A program performs a task when the shell starts it.
A terminal is like a workspace window; the shell is the command interpreter working inside it. The shell can run many programs during a single session.
Examples in this book use Bash, a common Linux shell. If you are unsure which shell is running, enter:
bash
bash
This starts a Bash shell inside your current shell, assuming Bash is installed. You can continue the exercises there. When you eventually type exit, you return to the shell that started it.
When the shell is ready to read a command, it normally displays a prompt. It might look something like this:
text
alex@workstation:~$
Your prompt will probably look different. It may show a username, a machine name, a directory, or other information. The ending character is often $ for a regular user. A prompt ending in # commonly indicates an administrative shell; use a regular user account for these exercises when possible.
The prompt is not part of the command. In this book, command examples appear without it:
bash
pwd
Type pwd and press Enter. Do not type a dollar sign merely because you see one in a prompt elsewhere.
The command prints the path of your current working directory: the location the shell uses as its starting point for many file operations. Your result might be:
text
/home/alex
Your username and path may differ. Linux paths are case-sensitive, so Practice and practice refer to different names.
After the output, the shell displays another prompt. That new prompt means the shell is ready for another command. It does not necessarily mean that the previous command succeeded; later in the book, you will learn a more reliable way to check.
A dedicated directory keeps the files you create in this book together. The following command takes you to your home directory:
bash
cd
Now check your location:
bash
pwd
Create a directory for the exercises:
bash
mkdir linux-pipes-practice
The command name is mkdir, short for “make directory.” The word after it is the name of the directory to create. If the command succeeds, it will usually produce no visible output. That silence is normal.
If you receive a message saying the directory already exists, choose a different name, such as linux-pipes-practice-new, and use that name in the next command. If you receive a permissions error, check that you are in a location where your account can create files.
Move into the new directory:
bash
cd linux-pipes-practice
Then confirm where you are:
bash
pwd
The displayed path should end in linux-pipes-practice, unless you chose another name. For example:
text
/home/alex/linux-pipes-practice
You can inspect the directory with:
bash
ls
A newly created directory will normally have no visible entries, so ls may print nothing before the next prompt. This does not mean the terminal is broken. It means the command has no names to display.
For the exercises ahead, check your location with pwd whenever you are uncertain. A practice directory is helpful because relative filenames refer to the current working directory. It does not prevent a command from reaching other locations, so read commands before running them, especially when they change or remove files.
A simple command line often contains a command name followed by one or more arguments. Arguments provide information that the command uses: a directory name, text to print, or an option that changes its behavior.
Consider:
bash
mkdir notes
Here, mkdir is the command name, and notes is an argument. The argument tells mkdir which directory to create. Run the command inside your practice directory, then list the contents:
bash
ls
You should see:
text
notes
The shell separates the command name and arguments as it interprets the line. For these simple examples, spaces mark the boundaries between words. In Chapter 9, you will see how quoting lets a single argument contain spaces and how the shell can transform parts of a command line before a program starts.
You can give some commands more than one argument:
bash
mkdir drafts reports
This asks mkdir to create two directories, drafts and reports. Check the result:
bash
ls