PROJET AUTOBLOG


Planet-Libre

source: Planet-Libre

⇐ retour index

blog-libre : Mémo Parameter Expansion

vendredi 3 mars 2017 à 07:00

Dernière mise à jour : 03/03/2017

Parameter Expansion est un ensemble d’opérations intégré à Bash et ksh, il peut parfois avantageusement remplacer l’utilisation de cut/sed/awk (mais il ne remplace pas toutes les possibilités que ces commandes offrent) :

J’ai fait certains choix pour ce mémo :

Les meilleures ressources sur le sujet :
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
http://wiki.bash-hackers.org/syntax/pe
http://mywiki.wooledge.org/BashFAQ/073

String length

Afficher la longueur de la chaîne de caractères contenue dans la variable (#)

mavariable="Qu'il est beau mon mémo ho ho ho !"; echo ${#mavariable}
34 (la longueur en nombre de caractères contenue dans mavariable, les espaces sont comptés)

Search and replace

Remplacer la première occurence de firefox par chromium-browser (/firefox/chromium-browser)

mavariable="/usr/lib/firefox/firefox https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html"; echo ${mavariable/firefox/chromium-browser}
/usr/lib/chromium-browser/firefox https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

Remplacer toutes les occurences de firefox par chromium-browser (//firefox/chromium-browser)

mavariable="/usr/lib/firefox/firefox https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html"; echo ${mavariable//firefox/chromium-browser}
/usr/lib/chromium-browser/chromium-browser https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

Substring removal

Supprimer la chaîne de caractères la plus courte possible en partant du début jusqu’à ; (#*;)

mavariable="Augier:Cascador/Laurent;Damien;alter A1"; echo ${mavariable#*;}
Damien;alter A1

Supprimer la chaîne de caractères la plus longue possible en partant du début jusqu’à ; (##*;)

mavariable="Augier:Cascador/Laurent;Damien;alter A1"; echo ${mavariable##*;}
alter A1

Supprimer la chaîne de caractères la plus courte possible en partant de la fin jusqu’à : (%:*)

mavariable="Augier:Cascador/Laurent;Damien;alter A1"; echo ${mavariable%:*}
Augier

Supprimer la chaîne de caractères la plus longue possible en partant de la fin jusqu’à ; (%%;*)

mavariable="Augier:Cascador/Laurent;Damien;alter A1"; echo ${mavariable%%;*}
Augier:Cascador/Laurent

Supprimer la chaîne de caractères la plus courte possible en partant de la fin jusqu’à (espace) (% *)

mavariable="/usr/lib/firefox/firefox https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html"; echo ${mavariable% *}
/usr/lib/firefox/firefox

Supprimer la chaîne de caractères la plus courte possible en partant du début jusqu’à (espace) (#* )

mavariable="/usr/lib/firefox/firefox https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html"; echo ${mavariable#* }
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

Supprimer la chaîne de caractères la plus longue possible en partant du début jusqu’à / (##*/)

mavariable="/usr/lib/firefox/firefox https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html"; echo ${mavariable##*/}
Shell-Parameter-Expansion.html

Supprimer la chaîne de caractères la plus courte possible en partant du début jusqu’à // (#*//)

mavariable="/usr/lib/firefox/firefox https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html"; echo ${mavariable#*//}
www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

Supprimer la chaîne de caractères la plus courte possible en partant de la fin jusqu’à bash (%bash*)

mavariable="/usr/lib/firefox/firefox https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html"; echo ${mavariable%bash*}
/usr/lib/firefox/firefox https://www.gnu.org/software/

Supprimer la chaîne de caractères la plus courte possible en partant du début jusqu’à manual (#*manual)

mavariable="/usr/lib/firefox/firefox https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html"; echo ${mavariable#*manual}
/html_node/Shell-Parameter-Expansion.html

Supprimer la chaîne de caractères la plus courte possible en partant du début jusqu’à firefox (#*firefox)

mavariable="/usr/lib/firefox/firefox https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html"; echo ${mavariable#*firefox}
/firefox https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

Supprimer la chaîne de caractères la plus longue possible en partant du début jusqu’à firefox (##*firefox)

mavariable="/usr/lib/firefox/firefox https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html"; echo ${mavariable##*firefox}
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

Supprimer la chaîne de caractères la plus courte possible en partant de la fin jusqu’à firefox (%firefox*)

mavariable="/usr/lib/firefox/firefox https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html"; echo ${mavariable%firefox*}
/usr/lib/firefox/

Supprimer la chaîne de caractères la plus longue possible en partant de la fin jusqu’à firefox (%%firefox*)

mavariable="/usr/lib/firefox/firefox https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html"; echo ${mavariable%%firefox*}
/usr/lib/

Extract parts of a filename (substring removal)

Extraire uniquement le nom du fichier sans l’extension (%.*)

mavariable="bash_hackers.txt"; echo ${mavariable%.*}
bash_hackers

Extraire uniquement l’extension du fichier (##*.)

mavariable="bash_hackers.txt"; echo ${mavariable##*.}
txt

Extraire uniquement le chemin du fichier (%/*)

mavariable="/home/babar/Blog/bash_hackers.txt"; echo ${mavariable%/*}
/home/babar/Blog

Extraire uniquement le nom du fichier (##*/)

mavariable="/home/babar/Blog/bash_hackers.txt"; echo ${mavariable##*/}
bash_hackers.txt

Case modification

Convertir le premier caractère en majuscule (^)

mavariable="tintouli, carl et cloug sont gentils"; echo ${mavariable^}
Tintouli, carl et cloug sont gentils

Convertir tous les caractères en majuscule (^^)

mavariable="Tintouli, carl et cloug sont gentils"; echo ${mavariable^^}
TINTOULI, CARL ET CLOUG SONT GENTILS

Convertir le premier caractère en minuscule (,)

mavariable="TINTOULI, CARL ET CLOUG SONT GENTILS"; echo ${mavariable,}
tINTOULI, CARL ET CLOUG SONT GENTILS

Convertir tous les caractères en minuscule (,,)

mavariable="tINTOULI, CARL ET CLOUG SONT GENTILS"; echo ${mavariable,,}
tintouli, carl et cloug sont gentils

Inverser la casse du premier caractère (~)

mavariable="Tintouli, Carl et Cloug sont gentils"; echo ${mavariable~}
tintouli, Carl et Cloug sont gentils

Inverser la casse de tous les caractères (~~)

mavariable="tintouli, Carl et Cloug sont gentils"; echo ${mavariable~~}
TINTOULI, cARL ET cLOUG SONT GENTILS

Substring expansion

Se positionner au 5ème caractère puis récupérer les caractères jusqu’à la fin (:5)

mavariable="abcdef.wxyz"; echo ${mavariable:5}
f.wxyz

Se positionner au début puis récupérer 3 caractères (:0:3)

mavariable="abcdef.wxyz"; echo ${mavariable:0:3}
abc

Se positionner au 2ème caractère puis récupérer 4 caractères (:2:4)

mavariable="abcdef.wxyz"; echo ${mavariable:2:4}
cdef

Récupérer 3 caractères en partant de la fin (: -3)

mavariable="abcdef.wxyz"; echo ${mavariable: -3}
xyz

Se positionner au 6ème caractère puis récupérer les caractères jusqu’au 2ème caractère en partant de la fin (:6:-2)

mavariable="abcdef.wxyz"; echo ${mavariable:6:-2}
.wx

Se positionner au 10ème caractère en partant de la fin puis récupérer 2 caractères (: -10:2)

mavariable="abcdef.wxyz"; echo ${mavariable: -10:2}
bc

Se positionner au 7ème caractère en partant de la fin puis récupérer les caractères jusqu’au 3ème caractère en partant de la fin (: -7:-3)

mavariable="abcdef.wxyz"; echo ${mavariable: -7:-3}
ef.w

Se positionner au 9ème caractère en partant de la fin puis récupérer aucun caractère (: -9:0)

mavariable="abcdef.wxyz"; echo ${mavariable: -9:0}
(rien)

Enchaîner mais pas combiner

Il n’est pas possible de combiner les différentes opérations (substring removal, substring expansion, etc.) entre elles, on peut les enchaîner les unes après les autres.

mavariable="/home/babar/Blog/bash_hackers.txt"; tmp_result1=${mavariable##*/}; tmp_result2=${tmp_result1%.*}; result=${tmp_result2/hackers/winners}; echo $result
bash_winners

Pour rappel utiliser ; sur une ligne de commandes revient à faire ceci dans un script (évidemment on mettra des commentaires et en forme le tout).

mavariable="/home/babar/Blog/bash_hackers.txt"
tmp_result1=${mavariable##*/}
tmp_result2=${tmp_result1%.*}
result=${tmp_result2/hackers/winners}
echo $result
bash_winners

Gravatar de blog-libre
Original post of blog-libre.Votez pour ce billet sur Planet Libre.

Articles similaires