Shell introduction#

What is Linux ?#

  • Posix type Operating System

  • POSIX = Portable Operating System Interface (uniX type)

  • Same standards than HP-UX, Solaris, SunOS, Irix, etc.

  • Linux = kernel (written by Linus Torvalds in 1991) + GNU software (FSF founded by Richard Stallman) then GNU/Linux

Note

  • the kernel of the GNU OS is called GNU-Hurd

  • most of Unix-type OSes are mainly composed of GNU tools

Posix#

POSIX systems are composed of

  • a kernel : set of programs (drivers, daemons, etc.) that interact with the hardware

  • a shell : or command interpreter which becomes the low level interface between the user and the kernel

  • a high level UI : in general a graphic interface or GUI (X-11, X-server)

Shells#

There are a number of different shells: sh, csh, ksh, ash, bash, zsh…

All shells have a grammar leading to being programmable or scriptable. And although they share the same grammar, the syntax of the various shells may sometimes differ.

80%-90% of any Linux distribution (Posix) is composed of shell scripts.

Bash#

bash and zsh have become the standard shells in Linux and macOS.

We chose bash for this entire tutorial since it is the most widespread shell. And from now on, every time we say “shell” we mean bash.

Information for macOS users

The default shell in the latest versions of macOS is now zsh. In order to find adequate support, we recommend that you switch to bash. To do this, open a terminal and run

chsh -s /bin/bash

After entering your password, close all terminal sessions and upon restart the default shell should have switched to bash.

The shell is the interface between the user and the kernel. We interact with it through a command line interface or CLI, also generally referred to as a terminal.

Terminal / CLI#

The terminal, or command-line interface gets as input the commands typed by the user.

It is an interactive command interpreter, that returns the result of the command and/or an error message.

To interact with the shell, we use commands and reserved keywords, environment variables and wildcards.

  • wildcard: a symbol or set of symbols with a particular meaning for the shell,

  • environment variable: a variable that changes the behavior of the shell, like PATH, PS1, SESSION, HOME, SHELL, USER, TERM, among others.

Bash login scripts#

In the terminal, the user usually sends a single-line command. To write more complex execution schemes, or a series of commands, one switches to scripts, text files written with the shell syntax.

The first shell scripts one has to deal with are the login scripts that customize the shell behavior

  • .bash_profile, sourced at the start of a new session on the machine,

  • .bashrc, sourced at the start of a new shell(= new terminal window)

Among the customizations, one can find most commonly the setting of environment variables and the creation of aliases.

Aliases#

Aliases are a way of renaming commands or series of commands with options under new variable name so they can be used more efficiently.

alias gs="git status"

Sometimes it serves the purpose of adding default options to certain commands such as adding colors to the ls command

alias ls="ls --color"

The PATH#

The PATH is an environment variable that defines where the shell will look for executable scripts (we will see later what it means). Therefore, the PATH looks like a series of absolute path to directories on the machine, where scripts can exist.

PATH="/usr/local/bin:/usr/bin"

The resolution of the path is ordered, so that if the same executable name exists in different directories, the directory that comes first in the PATH prevails. In the exemple above, if there is an executable called myprogram in /usr/bin and an other one in /usr/local/bin, if one enters

myprogram

in a given terminal with the above PATH, it will call myprogram from /usr/local/bin.

Warning

Be very careful when setting your PATH manually since it can lead to a loss of control of the terminal session.

The best way of dealing with your PATH is to append directories to it

PATH="${PATH}:/my/new/bin"  # this will append /my/new/bin to the existing PATH

The prompt PS1#

Another important environment variable, under the name PS1, is the prompt, the few characters at the beginning of each line in the terminal.

It usually provides information on the username, the host, the current directory, and looks like

user@host:/path/to/the/current/dir$

But it is actually fully customizable, and can have dynamic sections that will appear only in certain situations (e.g last command successful) or certain directories (e.g. directory under git versioning).

Tip

Go to https://bashrcgenerator.com/ to create your own personalized prompt in a breeze.

bash-git-prompt#

git is a tool for versioning code, and is widely used in the scientific community.

When a directory is versioned using git, the changes to each file in the directory are tracked, to preserve their history.

We will see how git works a bit further down this tutorial but here is a perfect spot to let you know about bash-git-prompt, a tool that appends relevant git information (current branch name, difference with remote branch, number of modified files, etc.) to the end of your prompt.

user@host:/path/to/code [main ↓·1|] $  # here the user is one commit behind on the main branch

It is very useful for software development!

Follow the installation procedure to add it to your prompt.