Working with the Linux shell

In this section, we will learn how to work in the shell efficiently. We will introduce some important practices and techniques that will improve your productivity and make you a faster shell command hacker. This can make you a happier person because, eventually, you will be able to advance to feeling very comfortable working in the shell. Please note, in this section, we will show you a lot of keyboard shortcuts. Learning keyboard shortcuts is like learning any other craft, you begin slowly and gradually, because learning too many new skills at once can leave you overwhelmed and make you forget more quickly than learning in smaller chunks. My tip is to start by learning the first three to four command editing shortcuts and then incorporate more from day to day or week to week. We will start with the command editing shortcuts. Now, if you don't know any command editing shortcuts at all, let's recap what you probably know so far on how to type and edit text in the command line.

The first shortcut for moving the position of the cursor is that you can use the left and the right arrow keys, which are helpful to edit the text you wrote to insert or delete characters at a specific position. But if this were all that one could do in the shell, working in the shell would be very inefficient, because single-character cursor movement is very slow. Also, every time a command gets executed with a typo or the command needs to be rerun with a small difference, such as changing one option, the complete command needs to be retyped from beginning to end.

To be a lot more efficient, let's introduce some very important command editing shortcuts for your everyday work with Linux:

Using the Alt key in some terminal emulators such as the Xfce4 Terminal is reserved for menu accessibility. So, you first have to disable the Alt key as a menu shortcut in the preferences before you can use it as a shortcut.

All of the command editing keyboard shortcuts we just discussed here are only the most important and efficient ones for your everyday daily use, and there are many, many more.

In order to get a full list of all of the Bash keyboard shortcuts, do the following:

For example, C-k stands for kill-line, which kills the text from the point to the end of the line. Alt + T is used to swap words, M-u to make words uppercase, and M-l to make words lowercase.

Now, let's move on to the command completion shortcuts. The most important command completion shortcut is the Tab key on your keyboard. It tries to guess and autocomplete the command you are about to type. It is very useful and speeds up typing commands tremendously, but don't overdo it when using this key, it can only print the full unique command name if there are no alternatives available. Type pass and press the Tab key; it will autocomplete the name passwd as there are no other programs with this full name available. Type pa and press the Tab key; this will give you several results as no unique name can be found. Type yp and press the Tab key; this will autocomplete to a long name as this is the only variant available. The Tab short key autocompletes commands by default; to autocomplete other things, such as filenames, use the Alt + / key. More can be found in the corresponding section in the Bash man page.

Now, let's look at to the command recall shortcuts. The Linux shell has a very nice feature available, which is the history command. This is a system for storing and retrieving all the commands typed into the shell. By default, on a CentOS 7 system, the last thousand commands are stored. This number can also be changed. The command-line history is a very useful feature to save time, by not doing repetitive typing, or to see how a specific command has been executed some time ago. To print out the current history, type history and press Enter. If you want to re-execute a command from this list use the exclamation mark and the corresponding number. Two exclamation marks run the last command from the history. Another exclamation mark notation can be used to extract specific arguments from history commands. This will extract the third argument from the history command, 166, as shown in the following screenshot:

Another very useful history feature is to recall the last command:

Next, we need to know how to work with programs and processes. First, we will discuss how to abort any running program. This is important if you need to quit a command because it is unresponsive or you've made a mistake and want to stop it. For example, let's type the cat command, which will just run forever. Let's ignore what this command is doing at the moment. This leaves the shell unresponsive because cat never finishes running in the forefront of our shell and runs forever. To get back to the shell prompt so we can type in new commands and work again, we need to exit the command while it is running. To do so in the shell, we can use a special key combination that exits the current foreground process. Press Ctrl + C.

This is a very important key shortcut and it should be memorized: Ctrl + C.

You can also suspend a program, which is like pausing its processing and putting it into the background so you can work in the shell again. This can be done as follows:

  1. Press Ctrl + Z. If you later want to continue the program running in the foreground, type fg and press Enter.
  2. You can also put it in the background using the bg command while it is suspended. Now the program runs in the background and you can work in the foreground.
  3. The easiest way to exit this program running in the background is to put it into the foreground and then use Ctrl + C to abort it.
  4. The next very useful command is to press Ctrl + L, which clears the screen and has the same effect as the clear command.
  5. The very last useful command we will learn here is to press Ctrl + D, which closes the Bash shell. This is similar to typing the exit command.