Shell Basics ============ The shell enables you to interact with the operating system and perform a wide variety of tasks. Learning to use the shell can not only speed up your workflow, but also enable you to better understand how your computer works. In this first section, we will very briefly cover some of the basics of the shell. In the next sections (today), you will learn how to use the shell. Terminology ----------- - The **terminal** is a window that allows you to interact with a shell. - The **shell** is a program that runs in the terminal. It interprets your commands and executes them. **bash**, **zsh** and **fish** are examples of shells. On Linux systems, you can generally open a terminal by pressing ``Ctrl+Alt+T``. You can see which shell you are using by running ``echo $SHELL`` or ``echo $0``. Command Overview ---------------- .. figure:: ../images/tutorial2/shellbasics/cmd_overview.png - **Command Prompt**: The command prompt shows some (configurable) info and shows the user that it is ready to take commands. - **User Command**: The first word you type is the command you want to run. - **Command Parameters**: Parameters can be options (``-a``, ``-l``), strings (``"hello world"``)... Most commands have manual pages or provide help options.
Try e.g., ``man ls`` or ``ls --help``. How Does One Command Execute? ----------------------------- Commands that you type in the shell are simply programs that are executed by the shell. These programs are located somewhere on your computer. Let's first quickly understand how the file system is organized on a Linux system. File System Overview (Linux) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - **The root directory**: The file system in Linux is organized as a tree. The root of the tree is ``/``. This is the equivalent of ``C:\`` on Windows. - **The path of a file**: The location of a file is called the *path* to that file. To specify a path, you can either use an *absolute path* (starting from the root directory):: /home/student/Pictures/my_goofy_dog.jpg or a *relative path* (starting from the current directory):: ./Pictures/my_goofy_dog.jpg - **Special directories**: The current directory is represented by "``.``" and the parent directory is represented by "``..``". Sometimes, you will also see the tilde "``~``" used in paths. This is the equivalent of the home directory of the current user. - **The home directory**: Each user has a home directory. This is where the user's files are stored. In the VM, the home directory of the user ``student`` is ``/home/student``. You can see the path to your home directory by running ``echo $HOME`` (or even ``echo ~``). The PATH Environment Variable ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Let's come back to the execution of commands. As said before, commands are simply programs that are executed by the shell. If you want to execute a program, you can provide the path to this program, and it will be executed. For example, to run ``git``, you could type: .. code-block:: none /usr/bin/git To run a ``hello_world`` program in the current directory, you could type:: ./hello_world However, typing the full path to a program every time you want to run it is tedious. This is where the ``PATH`` environment variable comes in. The ``PATH`` variable is a list of directories that the shell searches when you type a command. You can see the value of the ``PATH`` variable by running: .. code-block:: bash $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin The directories are separated by colons. As you can see, the ``/usr/bin`` directory, which contains the ``git`` program, is in the ``PATH``. Therefore, when you type ``git`` without specifying the full path, the shell is able to find it. .. important:: For the exam, there is a question about what happens when you type a command in a terminal. You do not have to talk in depth about the ``PATH`` when answering this question. The behavior of the shell (a user space program) is not what really Mr. Mathy expects you to know, but rather what happens in terms of the OS (system calls, processes, virtual memory, etc.). Try using, for instance, the ``strace`` command (e.g. ``strace YOUR_BINARY``) to see the list of system calls that are made *by* ``YOUR_BINARY``. The first system call is one of the things that interests you. Is this really the first system call that results from the execution of the process? (the anwser is no |:slight_smile:|) .. note:: - It is possible to know the full path of a command by running ``whereis ``. To know exactly what is being executed when you type a command, you can run ``type ``. - Some commands are built into the shell and do not lead to the execution of a separate program on the file system. These are called built-in commands. Examples include ``cd``, ``echo``, ``pwd``. For example, try running ``type cd``, which is a built-in command and then ``type git``, which is not.