====== Linux ====== ===== Fonctions d'affichage ===== \\ ==== Commande 'echo' ==== * Syntaxe // Affiche une chaîne de caractères $ echo "Hello world" Hello world // Affiche le contenu d'une variable $ var="commande echo" $ echo $var commande echo // Ne pas afficher le saut de ligne final $ echo -n bonjour bonjour$ // Insérer une tabulation $ echo -e "\tune tabulation avec echo" une tabulation avec echo // Insérer un saut de ligne $ echo -e "ligne 0\nligne 1\nligne 2" ligne 0 ligne 1 ligne 2 // Retour chariot $ echo -e "La commande echo\rRetour" Retourmande echo // Retour arrière $ echo -e "backspac\be" backspae // Antislash $ echo "\\\\" \\ // Caractères ASCII (hexadécimal) $ echo -e "\x40" @ // Caractères ASCII (octal) $ echo -e "\0176" @ // Désactiver l’interprétation des séquences spéciales $ echo -E "test\tLinux" test\tLinux \\ ==== Commande 'printf' ==== * Syntaxe $ printf [-v var] "chaine" expr1 expr2 ..... exprn # "chaine" représente la chaîne qui sera affichée à l'écran. # Elle peut contenir des formats qui seront substitués par la valeur des expressions citées à sa suite. # Il doit y avoir autant de formats que d'expressions. # # l'option -v permet d'enregistrer la sortie dans une variable. * Formats de conversion %[flags][width][.precision]specifier ^Format ^Description ^ |**%s** |Affichage d'une chaîne (string) | |%20s |Affichage d'une chaine (string) sur 20 positions avec cadrage à droite | |%-20s |Affichage d'une chaine (string) sur 20 positions avec cadrage à gauche | ^ ^^ |**%d** |Affichage d'un entier (décimal) | |%3d |Affichage d'un entier (décimal) sur 3 positions avec cadrage à droite | |%03d |Affichage d'un entier (décimal) sur 3 positions avec cadrage à droite et complété avec des 0 à gauche | |%-3d |Affichage d'un entier (décimal) sur 3 positions avec cadrage à gauche | |%+3d |Affichage d'un entier (décimal) sur 3 positions avec cadrage à droite et affichage systématique du signe | ^ ^^ |**%f** |Affichage d'un nombre flottant (float) | |%10.2f |Affichage d'un nombre flottant sur 10 positions dont 2 décimales | |%+010.2f |Affichage d'un nombre flottant sur 10 positions dont 2 décimales, complété par des 0 à gauche, avec cadrage à droite et affichage systématique du signe | ^ ^^ |**%b** |Print the argument while expanding backslash escape sequences | |**%q** |Print the argument shell-quoted, reusable as input | |**%o** |Print the argument as an unsigned octal integer | |**%x, %X** |Print the argument as an unsigned hexadecimal integer | |**%e, %E** |Print the argument as a floating-point number in exponential notation | |**%a, %A** |Print the argument as a floating-point number in hexadecimal fractional notation | |**%g, %G** |Print the argument as a floating-point number in normal or exponential notation | |**%c |Print the argument as a single character | |**%%** |Print a literal % symbol | * Caractères d'échappement Ces caractères sont utilisables dans la "chaîne" ^Format ^Description ^ |\\\\ |Displays a backslash character | |\b |Displays a backspace character | |\n |Displays a new line | |\r |Displays a carriage return | |\t |Displays a horizontal tab | |\v |Displays a vertical tab | * Exemples $ article="Livres" $ quantite=3 $ prix=3,5 $ printf "%-20s***%03d***%+10.2f\n" $article $quantite $prix Livres ***003*** +3,50 The following command prints the number 100 in three different number systems: $ printf "Decimal: %d\nHex: %x\nOctal: %o\n" 100 100 100 Decimal: 100 Hex: 64 Octal: 144 $ printf "%0*d" 10 5 # ou $ printf "%000000000d" 5 00000000005