See: Unofficial bash strict mode
Editer le fichier /etc/DIR_COLORS et remplacer
DIR 00;34 # directory
par
DIR 00;32 # directory
$((a + 200)) # Add 200 to $a
$(($RANDOM%200)) # Random number 0..199
echo -n "Proceed? [y/n]: "
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
if [[ "$1" == '--' ]]; then shift; fi
python hello.py > output.txt # stdout to (file)
python hello.py >> output.txt # stdout to (file), append
python hello.py 2> error.log # stderr to (file)
python hello.py 2>&1 # stderr to stdout
python hello.py > output.txt 2>&1 # stderr to stdout (file and no screen output)
python hello.py 2>/dev/null # stderr to (null)
python hello.py &>/dev/null # stdout and stderr to (null)
| Expression | Description |
|---|
$HOME | chemin du répertoire personnel de l’utilisateur |
$OLDPWD | chemin du répertoire précédent |
$PATH | liste des chemins de recherche des commandes exécutables |
$PPID | PID du processus père du shell |
$PS1 | invite principale du shell |
$PS2 | invite secondaire du shell |
$PS3 | invite de la structure shell “select” |
$PS4 | invite de l’option shell de débogage “xtrace” |
$PWD | chemin du répertoire courant |
$RANDOM | nombre entier aléatoire compris entre 0 et 32767 |
$REPLY | variable par défaut de la commande “read” et de la structure shell “select” |
$SECONDS | nombre de secondes écoulées depuis le lancement du shell |
| Expression | Description |
|---|
$? | Exit status de la dernière tache |
$! | PID de la dernière tache en BG |
$$ | PID du shell |
$0 | Nom du script shell |
| Expression | Description |
|---|
$# | Nombre d’arguments |
$* | Tous les arguments |
$@ | Tous les arguments, en commencant par le premier |
$1 | Premier argument |
$_ | Argument de la dernière commande |
| Expression | Description |
|---|
${FOO:-val} | $FOO, or val if unset (or null) |
${FOO:=val} | Set $FOO to val if unset (or null) |
${FOO:+val} | val if $FOO is set (and not null) |
${FOO:?message} | Show error message and exit if $FOO is unset (or null) |
Omitting the : removes the (non)nullity checks, e.g. ${FOO-val} expands to val if unset otherwise $FOO.
| Expression | Description |
|---|
${FOO:0:3} | Substring (position, length) |
${FOO:(-3):3} | Substring from the right |
| Expression | Description |
|---|
${#FOO} | Taille de la variable $FOO |
| Expression | Description |
|---|
{A,B} | Same as A B |
{A,B}.js | Same as A.js B.js |
{1..5} | Same as 1 2 3 4 5 |
variable
echo ${STR,} #=> "hELLO WORLD!" (lowercase 1st letter)
echo ${STR,,} #=> "hello world!" (all lowercase)
echo ${STR^} #=> "Hello world!" (uppercase 1st letter)
echo ${STR^^} #=> "HELLO WORLD!" (all uppercase)
echo ${name/J/j} #=> "john" (substitution)
echo ${name:0:2} #=> "Jo" (slicing)
echo ${name::2} #=> "Jo" (slicing)
echo ${name::-1} #=> "Joh" (slicing)
echo ${name:(-1)} #=> "n" (slicing from right)
echo ${name:(-2):1} #=> "h" (slicing from right)
echo ${food:-Cake} #=> $food or "Cake"
répertoire
echo ${STR%.cpp} # /path/to/foo
echo ${STR%.cpp}.o # /path/to/foo.o
echo ${STR%/*} # /path/to
echo ${STR##*.} # cpp (extension)
echo ${STR##*/} # foo.cpp (basepath)
echo ${STR#*/} # path/to/foo.cpp
echo ${STR##*/} # foo.cpp
echo ${STR/foo/bar} # /path/to/bar.cpp
echo ${STR:6:5} # "world"
echo ${STR: -5:5} # "world"
BASE=${SRC##*/} #=> "foo.cpp" (basepath)
DIR=${SRC%$BASE} #=> "/path/to/" (dirpath)
| Code | Description |
|---|
${FOO%suffix} | Supprimer suffix |
${FOO#prefix} | Supprimer prefix |
| --- | --- |
${FOO%%suffix} | Supprimer long suffix |
${FOO##prefix} | Supprimer long prefix |
| --- | --- |
${FOO/from/to} | Remplacer premier match |
${FOO//from/to} | Remplacer tout |
| --- | --- |
${FOO/%from/to} | Remplacer suffix |
${FOO/#from/to} | Remplacer prefix |
Fruits=('Apple' 'Banana' 'Orange')
echo ${Fruits[0]} # Element #0
echo ${Fruits[-1]} # Last element
echo ${Fruits[@]} # All elements, space-separated
echo ${#Fruits[@]} # Number of elements
echo ${#Fruits} # String length of the 1st element
echo ${#Fruits[3]} # String length of the Nth element
echo ${Fruits[@]:3:2} # Range (from position 3, length 2)
echo ${!Fruits[@]} # Keys of all elements, space-separated
Fruits=("${Fruits[@]}" "Watermelon") # Push
Fruits+=('Watermelon') # Also Push
Fruits=( ${Fruits[@]/Ap*/} ) # Remove by regex match
unset Fruits[2] # Remove one item
Fruits=("${Fruits[@]}") # Duplicate
Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate
lines=(`cat "logfile"`) # Read from file
for item in “${!dict[@]}”
echo “$item => ${dict[$item]}”
On déclare sound en tant que dictionnaire (couple clé:valeur)
echo ${sounds[dog]} # Dog's sound
echo ${sounds[@]} # All values
echo ${!sounds[@]} # All keys
echo ${#sounds[@]} # Number of elements
unset sounds[dog] # Delete dog
for val in "${sounds[@]}"; do
for key in "${!sounds[@]}"; do
for ((i = 0 ; i < 100 ; i++)); do
cat file.txt | while read line; do
ps - elf | grep squid | grep -v grep
grep -E -v '^(#|$)' <fichier>
egrep -v '^(#|$)' <fichier>
egrep -i "apache|lsyncd" <file>
grep -i -e apache -e lsyncd <file>
if grep -q 'foo' ~/.bash_history; then
echo "You appear to have typed 'foo' in the past"
ls toto 2>&1 | tee -a toto.log;
echo ${PIPESTATUS[0]} #RC Avant pipe
awk 'sub(/^M/, "");1' <ficsource> <ficmaj>
awk -F"\t" '{ print $56 }'
awk 'BEGIN{print 1024 / 1000}'
sed 's/^M//g' <ficsource> <ficmaj>
sed -i 's/^M//g' <ficsource>
sed -e " //{N;s/Hxxxxx/Pxxxxx/g;} "\
sed -i '{:q;N;s/\n//g;t q}' days.txt
sed -i 's/^.\{10\}//g' fichier.txt
<code>sed -n 1p check_toto.txt
if [ -n "$(echo $var | sed 's/[0-9]//g')" ]; then
DIR="$(readlink -f "$(dirname "$0")/../data/transfers")"
Le script $(basename ${0}) est correctement exécuté
find . -name '*.mp4' | rename mp4 txt *
find / -xdev -type f -exec grep -i toto {} /dev/null \;
find . -type f -mtime +7 -name « *.trc » -exec rm -f {} \;
find . -type f -mtime +2 -size xx -exec rm {}; avec xx = 10M ou 10K par ex
find . -type f -mtime +2 -name /*/backup/*/ -exec rm {}; (enlever les / )
find . -type f -mtime +1 -exec rm
find . -type f -print | xargs grep -i « my_string_to_search »> /tmp/search.log 2>/dev/null;
find . -name '*.txt' -exec basename {} \;
git commit || echo "Commit failed"
if [[ -z "$string" ]]; then
elif [[ -n "$string" ]]; then
echo "String is not empty"
| Condition | Description |
|---|
[[ -z STRING ]] | Empty string |
[[ -n STRING ]] | Not empty string |
[[ STRING == STRING ]] | Equal |
[[ STRING != STRING ]] | Not Equal |
| --- | --- |
[[ NUM -eq NUM ]] | Equal |
[[ NUM -ne NUM ]] | Not equal |
[[ NUM -lt NUM ]] | Less than |
[[ NUM -le NUM ]] | Less than or equal |
[[ NUM -gt NUM ]] | Greater than |
[[ NUM -ge NUM ]] | Greater than or equal |
| --- | --- |
[[ STRING =~ STRING ]] | Regexp |
| --- | --- |
(( NUM < NUM )) | Numeric conditions |
| Condition | Description |
|---|
[[ -o noclobber ]] | If OPTIONNAME is enabled |
| --- | --- |
[[ ! EXPR ]] | Not |
[[ X && Y ]] | And |
| `[[ X | |
| Condition | Description |
|---|
[[ -e FILE ]] | Exists |
[[ -r FILE ]] | Readable |
[[ -h FILE ]] | Symlink |
[[ -d FILE ]] | Directory |
[[ -w FILE ]] | Writable |
[[ -s FILE ]] | Size is > 0 bytes |
[[ -f FILE ]] | File |
[[ -x FILE ]] | Executable |
| --- | --- |
[[ FILE1 -nt FILE2 ]] | 1 is more recent than 2 |
[[ FILE1 -ot FILE2 ]] | 2 is more recent than 1 |
[[ FILE1 -ef FILE2 ]] | Same files |
if [[ -z "$string" ]]; then
elif [[ -n "$string" ]]; then
echo "String is not empty"
echo "This never happens"
echo "$a is smaller than $b"
if [[ -e "file.txt" ]]; then