URL: https://linuxfr.org/forums/programmationautre/posts/resolu-bash-utiliser-variable-dans-une-commande-du-style-result-commande-grep-variable Title: [RÉSOLU] Bash utiliser variable dans une commande du style result=$(commande | grep $variable) Authors: EauFroide Date: 2017年05月25日T19:18:33+02:00 License: CC By-SA Tags: Score: 3 J'ai du mal a formuler cette question à Google alors je vous la formule a vous :P Le problème vient de la variable _$mountPoint_, en effet lorsque je place directement la valeur au lieu de la variable, cela fonctionne. Donc la question : comment suis-je censé spécifier la variable pour l'incorporer dans ce genre de commande ```bash result=$(mount | grep "$mountPoint" | wc -l) ``` exemple **FONCTIONNE** ```bash #!/bin/bash result=$(mount | grep "/media/monPath" | wc -l) if [ ${result} = "1" ];then echo "montage effectif" else echo "mont```age not work" fi ``` **NE FONCTIONNE PAS** ```bash #!/bin/bash mountPoint="/media/monPath" result=$(mount | grep "$mountPoint" | wc -l) if [ ${result} = "1" ];then echo "montage effectif" else echo "montage not work" fi ``` **NE FONCTIONNE PAS** ```bash #!/bin/bash mountPoint="/media/monPath" result=$(mount | grep "${mountPoint}" | wc -l) if [ ${result} = "1" ];then echo "montage effectif" else echo "montage not work" fi ``` **résolu** : merci à vous tous pour vos solutions et commentaires fort intéressant ! Solution dans les commentaires : https://linuxfr.org/forums/programmationautre/posts/bash-utiliser-variable-dans-une-commande-du-style-result-commande-grep-variable#comment-1702904 Voici le script final en suivant vos commentaires: ```bash #!/bin/bash pathMontage="/media/folder" result=$(eval "mount | grep " ${pathMontage} " | wc -l") if [ "$result" = "1" ];then echo "montage effectif" else echo "montage not work" fi ```

AltStyle によって変換されたページ (->オリジナル) /