Shell syntax#

When you’re working with the command line, you can use symbols or specific syntax to make your work easier, here is some of them.

1. User status#

Knowing what symbol is shown at the beginning of your line tells you what kind of user you are.

If you’re logged as a normal user, your line will begin with $.

user@machine:~$ 

If you’re logged as a root, your line will begin with #.

root@machine:~#

Tip

For the rest of the examples, we’ll consider that we are a normal user

2. Home#

The symbol ~ refers to the user’s home directory.

user@machine:~$ ls ~
Desktop Documents Pictures Videos Download

3. Comments#

Using # creates a comment, the instruction won’t be executed by the terminal.

user@machine:~$ #sudo apt update
user@machine:~$ 

4. Ampersand#

The ampersand symbol has two purposes.

When it’s puts at the end of an instruction, it runs that instruction in the background.

user@machine:~/Desktop$ gimp &
[1] 36261
user@machine:~/Desktop$ 

When use in double it allows to give a series of instructions that will be run sequentially.

user@machine:~$ cd Desktop && mkdir new_directory

Tip

This behavior is an AND, if the Nth instruction can’t be resolved, the process stop and no further instructions will be executed.

5. History#

Although we have now better way to get history (history, reverse-search) it can be useful to know that the exclamation mark ! has also access to history.

! Find command in your history (can be coupled it with a number).

user@machine:~$ !1 # Get the first command.
user@machine:~$ ls ~
user@machine:~$ !-2 # Get penultimate command.
user@machine:~$ cd Desktop && mkdir new_directory
user@machine:~/Desktop$ !! # Get the last command.
user@machine:~/Desktop$ gimp &

6. Redirection#

Rafters are used to interact with files.

  • > Redirects the standard output into a file.

    user@machine:~/Desktop/new_directory$ pwd > path.txt
    

Tip

If the file exists it’s overwritten otherwise it’s created

  • >> Append the standard output to the end of a file.

    user@machine:~/Desktop/new_directory$ cat path.txt >> very_big_file.txt
    
  • < Read content of a file into prompt.

    user@machine:~/Desktop/new_directory$ < path.txt
    /home/user/Desktop/new_directory
    
  • << Creates a block of code with delimiter called heredoc [command] << delimiter

    user@machine:~/Desktop/new_directory$ cat << terminate 
    heredoc> line 1
    heredoc> another line
    heredoc> terminate
    line 1
    another line
    
  • | redirects the output of a command in the input of another command

    user@machine:~/Desktop/new_directory$ history | grep gimp
    3336  sudo apt install gimp
    3812  gimp &
    

8. Standard data stream#

When you use your linux terminal, you have access to 3 data stream:

  • standard input stream stdin, associated to number 0

  • standard output stream stdout, associated to number 1

  • standard error stream stderr, associated to number 2

As we can manipulate them as files, we can have them interact using some instructions.

/dev/null is the void, anything that’s put in there goes to the limb and cannot be retrieved.

Let’s take the example of a command that returns both standard and error messages.

user@machine:~$ apt update

Lecture des listes de paquets... Fait

E: Impossible d'ouvrir le fichier verrou /var/lib/apt/lists/lock - open (13: Permission non accordée)
E: Impossible de verrouiller le répertoire /var/lib/apt/lists/
W: Problème de suppression du lien /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission non accordée)
W: Problème de suppression du lien /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission non accordée)

If we redirect 1 in /dev/null, we won’t see any standard output, just the errors.

user@machine:~$ apt update 1> /dev/null

E: Impossible d'ouvrir le fichier verrou /var/lib/apt/lists/lock - open (13: Permission non accordée)
E: Impossible de verrouiller le répertoire /var/lib/apt/lists/
W: Problème de suppression du lien /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission non accordée)
W: Problème de suppression du lien /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission non accordée)

Tip

that also work with > only

apt update > /dev/null

With 2, we throw all the errors output and we’re left with standard output.

user@machine:~$ apt update 2> /dev/null 
Lecture des listes de paquets... Fait

Finally, we redirect the standard output into /dev/null.

We also we redirect (>) the errors flux (2) into the standard flux (&1).

In the end nothing appear.

user@machine:~$ apt update > /dev/null 2>&1
user@machine:~$ 

Tip

the syntax &1 is here to avoid the creation of a file called 1 in which the output of 2 will be stored

9. Anything you want#

The * symbol mean that the command will be executed with whatever can fit in place of the *.

user@machine:~/Desktop/new_directory$ rm *.txt
'path.txt' supprimé
'very_big_file.txt' supprimé
'unsorted_name.txt' supprimé

Warning

The following code is an actual bash program (and a rather evil one :)

:(){ : | : &};:

Advice: do not run it in your terminal before you have safely saved all current work.

Try to guess what it does as an exercice, then head here for a detailed explanation.