Table of Contents

Bash Cheatsheet

Special parameters

Source

Parameters expansion

Source

Work with file paths

Output of a shell command

Mastering history

Source

Redirection

Deux trois trucs intéressants sur les redirections

The problem with cats ...

Take a simple redirection example such as this:

cat file1 | tr -d '\015' > file2

You've wasted a process on cat, since you could accomplish the same thing (and have it execute faster!) by doing this:

tr -d '\015' < file1 > file2

Sending stderr through a pipe

Sending only stderr down a pipe, while having stdout still go to the screen, is an interesting trick. It can be done by passing the stdout and stderr file descriptors to temporary file descriptors, and basically playing a game of 3 card monte with the values:

(binary 3>&1 1>&2 2>&3 3>&-) | mail me@somewhere.org &

Fonctions

func_name () { cmd1; cmd2 }

On retrouve cette syntax au coeur de la fameuse fork bomb bash :(){ :|:& };:: est le nom de la fonction. L'utilisation du pipe et des : est a priori seulement là pour obfusquer un peu plus la syntaxe (:) et la raccourcir (|). f(){f&;f&;};f fait quaisment la même chose.