Varibles bash
De Linuxmemo.
(→Expension de variable) |
(→Expension de variable) |
||
Ligne 83 : | Ligne 83 : | ||
${parameter:=defaultValue} Set default shell variables value | ${parameter:=defaultValue} Set default shell variables value | ||
${parameter:?"Error Message"} Display an error message if parameter is not set | ${parameter:?"Error Message"} Display an error message if parameter is not set | ||
- | ${#var} Find the length of the string | + | ${#var} Find the length of the string |
- | ${var%pattern} Remove from shortest rear (end) pattern | + | ${var%pattern} Remove from shortest rear (end) pattern |
- | ${var%%pattern} Remove from longest rear (end) pattern | + | ${var%%pattern} Remove from longest rear (end) pattern |
- | ${var:num1:num2} Substring | + | ${var:num1:num2} Substring |
- | ${var#pattern} Remove from shortest front pattern | + | ${var#pattern} Remove from shortest front pattern |
- | ${var##pattern} Remove from longest front pattern | + | ${var##pattern} Remove from longest front pattern |
- | ${var/pattern/string} Find and replace (only replace first occurrence) | + | ${var/pattern/string} Find and replace (only replace first occurrence) |
- | ${var//pattern/string} Find and replace all occurrences | + | ${var//pattern/string} Find and replace all occurrences |
- | ${!prefix*} Expands to the names of variables whose names begin with prefix. | + | ${!prefix*} Expands to the names of variables whose names begin with prefix. |
+ | |||
${var,} | ${var,} | ||
- | ${var,pattern} Convert first character to lowercase. | + | ${var,pattern} Convert first character to lowercase. |
+ | |||
${var,,} | ${var,,} | ||
- | ${var,,pattern} Convert all characters to lowercase. | + | ${var,,pattern} Convert all characters to lowercase. |
+ | |||
${var^} | ${var^} | ||
- | ${var^pattern} Convert first character to uppercase. | + | ${var^pattern} Convert first character to uppercase. |
+ | |||
${var^^} | ${var^^} | ||
- | ${var^^pattern} Convert all character to uppercase.. | + | ${var^^pattern} Convert all character to uppercase.. |
https://www.cyberciti.biz/tips/bash-shell-parameter-substitution-2.html | https://www.cyberciti.biz/tips/bash-shell-parameter-substitution-2.html |
Version actuelle en date du 12 septembre 2019 à 12:22
Sommaire |
[modifier] Manipulation de variables simples
var=val ou var="a b" affectation de la variable "var"
$var ou ${var} contenu de la variable "var"
${#var} longueur de la variable "var"
export var ou declare -x var exportation de la variable "var" vers les shells fils
set affichage de l'ensemble des variables définies dans le shell
unset var suppression de la variable "var"
[modifier] Paramètres positionnels et arguments
$0 nom du script
$1 $2 ... ${10} paramètres positionnels (1, 2 et 10)
$# nombre de paramètres positionnels
$* ou $@ ensemble des paramètres positionnels, équivalant à $1 $2 ... ${n}
"$*" ensemble des paramètres positionnels, équivalant à "$1 $2 ... ${n}"
"$@" ensemble des paramètres positionnels, équivalant à "$1" "$2" ... "${n}"
[modifier] Variables spéciales
$$ PID du shell courant
$! PID du dernier travail lancé en arrière plan
$? code retour de la dernière commande
[modifier] Variables d'environnement
$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
[modifier] Typage des variables
- declare
Déclaration de variable/fonction et typage de la variable/fonction.
-a to make NAMEs indexed arrays (if supported) -A to make NAMEs associative arrays (if supported) -i to make NAMEs have the `integer' attribute -l to convert NAMEs to lower case on assignment -r to make NAMEs readonly -t to make NAMEs have the `trace' attribute -u to convert NAMEs to upper case on assignment -x to make NAMEs export
[modifier] Expension de variable
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
${parameter:-defaultValue} Get default shell variables value ${parameter:=defaultValue} Set default shell variables value ${parameter:?"Error Message"} Display an error message if parameter is not set ${#var} Find the length of the string ${var%pattern} Remove from shortest rear (end) pattern ${var%%pattern} Remove from longest rear (end) pattern ${var:num1:num2} Substring ${var#pattern} Remove from shortest front pattern ${var##pattern} Remove from longest front pattern ${var/pattern/string} Find and replace (only replace first occurrence) ${var//pattern/string} Find and replace all occurrences ${!prefix*} Expands to the names of variables whose names begin with prefix. ${var,} ${var,pattern} Convert first character to lowercase. ${var,,} ${var,,pattern} Convert all characters to lowercase. ${var^} ${var^pattern} Convert first character to uppercase. ${var^^} ${var^^pattern} Convert all character to uppercase..
https://www.cyberciti.biz/tips/bash-shell-parameter-substitution-2.html