Tableaux en bash¶
Création d'un tableau (et initialisation):¶
Tableau indicé:¶
# Création:
$ tableau_indi=()
$ declare -a tableau_indi
$ tableau_indi=( "un" "deux" "trois" "quatre" )
$ declare -a tableau_indi=( "un" "deux" "trois" "quatre" )
Les indices sont assignés automatiquement, à partir de 0.
$ tableau_indi=( "chene" "erable" [5]="saule" "hetre"
chene à l'indice 0, érable le 2, saule le 5, hetre le 7. Les indices 3 et 4 sont des chaînes vides.
Tableau associatif:¶
# Création:
$ declare -A tableau_asso
$ tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
$ declare -A tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
# Returns all indizes and their items (doesn't work with associative arrays)
$ echo ${tableau_asso[@]}
gland faine sirop
# Returns all items
$ echo ${tableau_asso[*]}
gland faine sirop
# Returns all indizes
$ echo ${!tableau_asso[*]}
erable chene hetre
# Number elements
$ echo ${#tableau_asso[*]}
3
# Length of $nth item
${#tableau_asso[$n]}
Tableau en lecture seule:¶
$ readonly -a tableau_indi_ro=( "un" "deux" "trois" "quatre" )
$ declare -r -a tableau_indi_ro=( "un" "deux" "trois" "quatre" )
$ readonly -A tableau_asso_ro=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
$ declare -r -A tableau_asso_ro=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
Affichage du tableau:¶
L’affichage de l’ensemble d’un tableau se fait avec la syntaxe ${montableau[*]}
ou${montableau[@]}
$ declare -a tableau_indi=( "un" "deux" "trois" "quatre" )
$ echo ${tableau_indi[@]}
un deux trois quatre
$ echo ${tableau_indi[*]}
un deux trois quatre
$ echo ${tableau_indi[2]}
deux
$ declare -A tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
$ echo ${tableau_asso[@]}
gland faine sirop
$ echo ${tableau_asso[*]}
gland faine sirop
$ echo ${tableau_asso[erable]}
sirop
https://ostechnix.com/bash-for-loop-shell-scripting/#loop-over-array-elements
Lecture d'un élément:¶
$ tableau_indi=( "un" "deux" "trois" "quatre" )
$ echo ${tableau_indi[2]}
deux
$ echo $tableau_indi
un # zsh: un deux trois quatre
$ declare -A tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
$ echo ${tableau_asso["erable"]}
sirop # !!!! rien sur zsh !!!!
$ echo ${tableau_asso[erable]}
sirop
Ajouter un élément:¶
$ tableau_indi=()
$ tableau_indi+=('cinq')
$ tableau_indi+=('six')
$ tableau_asso+=([pommier]="pomme")
$ tableau_asso[pommier]="pomme"
Modification d'un élément:¶
$ declare -a tableau_indi=( "un" "deux" "trois" "quatre" )
$ tableau_indi[0]="zero"
$ echo ${tableau_indi[@]}
zero deux trois quatre
Si le tableau n’existe pas, il sera créé comme un tableau indicé :
$ tableau_indi[1]="chene"
$ echo ${tableau_indi[1]}
chene
Il n’est pas possible de créer un tableau associatif en lui assignant un élément, il faut le déclarer explicitement (declare -A tableau_asso) avant l’assignation.
Afficher la liste des clés:¶
$ declare -a tableau_indi=( "un" "deux" "trois" "quatre" )
$ echo ${!tableau_indi[@]}
0 1 2 3 #zsh: event not found: tableau_indi[@] # !!!
$ declare -A tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
$ echo ${!tableau_asso[@]}
erable chene hetre # zsh: event not found: tableau_asso[@] # !!!
Afficher la liste des clés/valeurs:¶
$ for val in ${!tableau_indi[@]}
do
echo "index = ${val} , value = ${tableau_indi[$val]}"
done
index = 0 , value = un
index = 1 , value = deux
index = 2 , value = trois
index = 3 , value = quatre
$ for elem in "${!tableau_asso[@]}";
do
echo "key : ${elem}" - - "value: ${tableau_asso[${elem}]}";
done
key : erable - - value: sirop
key : chene - - value: gland
key : hetre - - value: faine
Obtenir la taille d'un tableau:¶
$ declare -a tableau_indi=( "un" "deux" "trois" "quatre" )
$ echo ${#tableau_indi[@]}
4
$ declare -A tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
$ echo ${#tableau_asso[@]}
3
Supprimer un élément:¶
$ declare -a tableau_indi=( "un" "deux" "trois" "quatre" )
$ echo ${tableau_indi[@]}
un deux trois quatre
$ echo ${#tableau_indi[@]}
4 # 4 éléments
$ unset tableau_indi[1]
# zsh: no matches found: tableau_indi[1]
$ echo ${!tableau_indi[@]}
0 2 3 # zsh: event not found: tableau_indi[@]
$ echo ${tableau_indi[@]}
un trois quatre
$ echo ${#tableau_indi[@]}
3 # 3 éléments
$ declare -A tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
$ echo ${tableau_asso[@]}
gland faine sirop
$ echo ${#tableau_asso[@]}
3 # 3 éléments
$ unset tableau_asso['erable']
# zsh: no matches found: tableau_asso[erable]
$ echo ${!tableau_asso[@]}
chene hetre # zsh: event not found: tableau_asso[@]
$ echo ${tableau_asso[@]}
gland faine
$ echo ${#tableau_asso[@]}
2 # 2 éléments
Vider un tableau:¶
$ declare -a tableau_indi=( "un" "deux" "trois" "quatre" )
$ echo ${tableau_indi[@]}
un deux trois quatre
$ declare -a tableau_indi=()
$ echo ${tableau_indi[@]}
$ declare -A tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
$ echo ${tableau_asso[@]}
gland faine sirop
$ declare -A tableau_asso=()
$ echo ${tableau_asso[@]}
Supprimer un tableau entier:¶
$ declare -a tableau_indi=( "un" "deux" "trois" "quatre" )
$ echo ${tableau_indi[@]}
un deux trois quatre
$ unset tableau_indi
$ echo ${tableau_indi[@]}
$ declare -A tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
$ echo ${tableau_asso[@]}
gland faine sirop
$ unset tableau_asso
$ echo ${tableau_asso[@]}
Vérifier si un élément est présent dans le tableau:¶
$ declare -A tableau_asso=( ['chene']="gland" ['erable']="sirop" ['hetre']="faine" )
$ if [[ -n "${tableau_asso[erable]}" ]]; then echo "Element is present"; else echo "Element not present"; fi
Element is present
Mettre la sortie d'une commande dans un tableau:¶
$ path_list=( $(echo $PATH | tr ":" "\n") )
$ echo ${path_list[@]}
/Users/bruno/.zinit/plugins/sei40kr---fast-alias-tips-bin /Users/bruno/.zinit/polaris/bin /Users/bruno/perl5/bin /Users/bruno/.nvm/versions/node/v18.13.0/bin /Users/bruno/Library/Python/3.9/bin /opt/homebrew/opt/grep/libexec/gnubin /opt/homebrew/opt/coreutils/libexec/gnubin /opt/homebrew/opt/ruby/bin /opt/homebrew/opt/unzip/bin /opt/homebrew/opt/ssh-copy-id/bin /opt/homebrew/opt/python/libexec/bin /opt/homebrew/bin /opt/homebrew/sbin /usr/local/bin /System/Cryptexes/App/usr/bin /usr/bin /bin /usr/sbin /sbin /usr/local/MacGPG2/bin /Library/Apple/usr/bin /Users/bruno/.cargo/bin /Users/bruno/.local/bin /opt/homebrew/opt/fzf/bin /Users/bruno/.yarn/bin /Users/bruno/.local/bin /Users/bruno/go/bin /Users/bruno/.zinit/plugins/kazhala---dotbare
Slicing¶
${array[@]:index:length}
$ declare -a tableau_indi=( "un" "deux" "trois" "quatre" )
$ echo ${tableau_indi[@]}
un deux trois quatre
# Affiche de la position 2 à la fin
$ echo ${tableau_indi[@]:2}
trois quatre
# Affiche 2 éléments à partir de la position 1
$ echo ${tableau_indi[@]:1:2}
deux trois
# Affiche 2 éléments à partir du début
$ echo ${tableau_indi[@]::2}
un deux # zsh: closing brace expected
Dernière mise à jour:
April 3, 2020