5.1. Shell#

The shell is a basic interface that allows users to communicate with the kernel and any installed programs. Whenever you open a terminal, the kernel starts an instance of a shell program, or shell “session”. When most people interact with computers, they do so through graphical user interfaces that they navigate using a mouse or their finger. The shell is completely text-based and designed for programmers. It has its own programming language of special commands to access kernel functionalities. For more information about terminals and shells, please refer to the relevant sections of the following textbook.

5.1.1. Some basic shell commands#

5.1.1.1. Help and information#

5.1.1.1.1. man <command>#

This is probably the most important command for students new to Linux and interacting with computers via the shell. The man command (short for “manual”) allows you to access documentation about all programs installed on a system. For example, to view documentation for the man command itself, you simply type man man to open its man page. You can scroll through man pages using the arrow keys or ctrl+f (move forward one page) and ctrl+b (move backward one page) and exit back to the command line by typing q. For more information about any of the other commands discussed in this chapter (or any chapter for that matter), consult these man pages!

5.1.1.1.2. apropos <keyword>#

This is probably the second most important command for students new to Linux and interacting with computers via the shell. If you type man apropos, its description is

apropos searches a set of database files containing short descriptions of system commands for keywords and displays the result on the standard output.

In other words, it searches all man-able programs for the keyword, and displays it on the screen. This can be useful if you know that a program description has a certain keyword or functionality, but not know the specific name of it.

5.1.1.3. Creating, viewing, and manipulating files and directories#

  • touch <desiredfilename>: create a new file.

  • cat <filename>: print contents of a file to the terminal.

  • mv /path/to/<filename> /desired/path/to/file/: move a file to a different directory. To move a directory instead of just a single file, use the flag -r.

  • cp <filename> <filecopyname>: make a copy of an existing file.

  • mdkir <desireddirectoryname>: create a new directory.

  • emacs <filename>: open the file “filename” in the EMACS editor. More on this in the following section.

  • wc <filename>: print newline, word, and byte counts for a file. To see just the word count, add the flag -w.

Commands as files within the path list: interesting resource to help with understanding of shell commands with an exercise you can follow!

5.1.1.4. Miscellaneous#

5.1.1.4.1. echo#

Display a line of text to standard out.

$ echo "Hello world"
Hello world

5.1.1.4.2. ctrl+c#

This will terminate whatever process/command is currently running in the shell. Great if you think your code may be stuck in an infinite loop or is just taking much longer than you thought it would to finish running.

5.1.1.4.3. ctrl+l#

This will clear your terminal and move the prompt up to the top of the screen.

5.1.1.5. Symbols#

5.1.1.5.1. |#

This is known as the “pipe” symbol and is used to redirect the output of one command into the input of a second command. For example, if I type ls .. | wc -w, the total number of files/directories in the .. directory will be printed to the command line.

5.1.1.5.2. >#

This symbol is used to assign a redirect-out to a command. For example, if I type: echo "Hello world" > hw.txt, instead of printing “Hello world” to the terminal, the right carrot symbol would direct that output into the file “hw.txt”.

5.1.1.5.3. <#

This symbol is used to assign a redirect-in to a command. For example, we can type cat < file.txt. In this situation, this is equivalent to cat file.txt.

5.1.1.5.4. &#

Typing the ampersand symbol & at the end of a command will result in that command being run in the background. This means that you will immediately see another prompt even if the command you run has not finished running.