Read a sample: 1. Getting Comfortable at the Command Line
A Linux command line can look like a blank page with a blinking cursor. That sparseness is useful: you ask the system a question or give it an instruction, and it responds with text. It can also feel unfamiliar at first, especially when you are not yet sure which parts of the screen are yours to type and which parts came from the computer.
You do not need to memorize a long list of commands to begin. In this chapter, you will open a terminal, recognize a prompt, enter a few small commands, and create a disposable directory for practice. Along the way, you will build a habit that matters more than typing quickly: pause long enough to understand what a command will do before pressing Enter.
The examples use an ordinary user account, not an administrator account. They assume a Linux system with a terminal and common command-line utilities available. Your screen may look different from the examples, and that is normal.
A terminal is the window or interface where you type commands and see text responses. The shell is the program that reads those commands and arranges for them to run. On many Linux systems, the shell is Bash, although others are common too.
If you are using a graphical desktop, look in the application menu for Terminal. Some desktops provide a keyboard shortcut such as Ctrl+Alt+T, but shortcuts vary. If you are connected to a remote Linux system, you may already be looking at a terminal session. If you are using a shared, managed, or training system, follow its instructions for opening a session and storing practice files.
When the shell is ready, it displays a prompt. A prompt might look like this:
text
morgan@linuxbox:~$
Your username, machine name, symbols, and colors may be different. A prompt often ends with $ for an ordinary user. A prompt ending in # commonly indicates a root shell, although prompts can be customized and are not a reliable identity test by themselves.
In this book, command examples appear in code blocks without the prompt:
bash
whoami
Type the command itself and press Enter. Do not type a $ that appears as part of a prompt shown in an example. When the command finishes, the shell normally displays another prompt, indicating that it is ready for your next instruction.
If you are unsure which account you are using, run:
bash
whoami
The output is typically your current username. If it says root, switch to an ordinary user account before continuing, or ask the person responsible for the system how to do so. The exercises in this chapter do not require root access.
Try this command:
bash
printf 'Hello, Linux!\n'
You should see:
text
Hello, Linux!
The command name is printf. A space separates it from the text that follows. The single quotes tell the shell to treat the enclosed characters as one piece of text. The \n tells printf to start a new line after the message.
You do not have to master quoting today. For now, notice that punctuation can have a purpose. Copy commands carefully, including spaces and quotation marks. Straight single quotes, as shown above, are different from the curved quotation marks sometimes inserted by word processors.
A command line generally has a command name followed by any options or other inputs the command needs. The shell reads the line after you press Enter. For example:
bash
date
This asks the system to display its current date and time. Your output will differ from anyone else’s. The important observation is the sequence: you enter a command, the system responds, and the prompt returns.
Not every successful command prints a message. Some do their work quietly and return you to the prompt. Conversely, an error message is not a verdict on your ability; it is information about something the system could not do. Read it, check what you typed, and avoid guessing at a more powerful command.
Before executing a line, look at the command name, its spaces, and anything that names a file or directory. If you spot a mistake, fix it while the line is still at the prompt.
The Backspace key usually removes the character to the left of the cursor. The left and right arrow keys usually move through the line so you can make a correction. On many shells, the up arrow recalls a previous command; review it before pressing Enter again rather than assuming it is still appropriate.
If you start typing a command and decide not to run it, press Ctrl+C. This normally cancels the current input and gives you a fresh prompt. Ctrl+C can also interrupt many running commands, though a program may handle the interruption in its own way.
Sometimes the shell shows a different prompt after Enter instead of running the command. For example, an opening quote without a matching closing quote can make the shell wait for more input. If that happens during an exercise, Ctrl+C is a straightforward way to cancel the unfinished line and start again.
The shell has a current working directory: the location it uses as the starting point for many file and directory names. Think of it as your current position while exploring, not as a restriction on where you can go.
To ask where you are, enter:
bash
pwd
A possible result is:
text
/home/morgan
Your result will almost certainly contain different…