Shell is a program that users employ to run commands for the operating system to execute. These can be very simple instructions such as copying or moving files, or long scripts that contain hundreds of commands. Users typically interact with a shell using a terminal window (also referred to as command line), which can be run from the same or a remote machine. There are different types of shells, but I will refer in this tutorial only to the Bourne-Again shell, which is the most commonly used. The Bourne-Again shell (or simply shell, as most people call it) is installed by default in any Linux or MacOS. The programming language used to write commands and scripts in shell is called bash.
The terminal window for Mac can be found inside the applications folder and in the subfolder Utilities. You can also open it by pressing the command and space bar in your keyboard to open the spotlight and then type Terminal. In Linux, the easiest way to open the terminal is to use the key combination Ctl + Alt + T.
Shell scripting is very powerful; it allows you to run any instruction on your computer or a remote machine. Things that normally would take hours to do could be executed in seconds. For example, if you have thousands of files inside a folder, which need to be renamed following a pattern, it would take forever to rename those files by hand. However, using the command line you could do it in seconds. When you write a shell script, you know exactly what and how you did things, and you will be able to replicate those commands on different files, datasets or programs.
However, as powerful as scripting is, it is also very dangerous. Through the terminal you will have access to any files or system resources if you have the right permissions, and the freedom to do anything with them. Inadvertent "small" typing errors such as adding an extra space in the remove command (rm) could delete everything in your computer (including the root directory) or any external drive connected to your machine. Similarly, the contents of any file could be mistakenly removed in less than a second. What makes it harder to prevent these errors is that many commands differ in name by only one letter.
Unfortunately, there is no way to completely protect your computer from potential mistakes that you may (and you will at some point) do when writing and running shell scripts. However, there are a couple of tips that you can follow in order to minimize the chances of making unfixable mistakes or to prevent any mistake from becoming a catastrophe:
First and most importantly, create constant backups of either your entire drive or the most important folders. Preferably save those backups in an external hard drive or remote machine, which won't be constantly connected to your computer. It is not a bad idea to have more than one backup if your data is not easily recoverable.
If you're using a shared computer or account, control access permissions to important files and folders. You can control for each file and folder who and what can be done. By managing permissions, you can allow a file to be read but not to be written (hence preventing it from being modified or deleted). Or you can authorize only a user or group of users for viewing, reading, writing or executing a file. In chapter 12 you will learn how to manage permissions.
The root user is an account in any Linux or Unix operating system (i.e. MacOS) that has access to absolutely all commands, files and directories. When you login as a root in the command line, you can modify, read and write protected folders and files. This is very helpful when installing software or modifying the default settings of your computer. However, coding as the root user can be extremely dangerous. It is very normal to make mistakes when coding and if you have all permissions, you could mistakenly delete, move or modify files that should not be touched. For this reason, it is advisable to only login as a root when strictly necessary and log out once you're done. More information about the root user will be covered in chapter 12.
Use aliases to protect from "dangerous" commands. Two of the most commonly used but dangerous commands are rm (to remove files) and mv (to move files). The mv command can be as harmful as the rm command because if you specify the wrong target or source paths, you can end up deleting a big number of files. When you use any of these two commands, always double check what you have written before clicking enter.
Be careful when copy pasting commands from a website into the terminal. There can be errors that will be executed right away if the text you're copying contains a carriage return at the end of the line. It is better to paste into a shell script and then either execute the shell script or copy from the shell script into the command line. Although generally not dangerous, you might also run into errors when copy pasting from Microsoft word or other editors that modify symbols such as dash or underscore and convert them into new symbols that bash won't understand.