Newer
Older
cours-unix-shell / UnixShell_cours2018 / paramShell.fre.html
<html>
  <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <link REL="STYLESHEET" TYPE="text/css" TITLE="Style DILIB" HREF="../../dilib.css">
    <style type="text/css">
.remarque { font-size: small; }
    </style>
    <title>Param&egrave;tres - Shell - UNIX</title>
    <link rel="copyright" href="http://www.inist.fr/"><link rel="author" href="http://dilib.inist.fr/membres.html"><link rel="chapter" href="caracteristiques.fre.html" title="UNIX"><link rel="chapter" href="X.fre.html" title="X Window"><link rel="chapter" href="redirPipes.fre.html" title="Rappels"><link rel="chapter" href="newCommandsUser.fre.html" title="Commandes UNIX"><link rel="chapter" href="shellVars.fre.html" title="Shell"><link rel="chapter" href="sed.fre.html" title="Sed"><link rel="chapter" href="make.fre.html" title="Make"><link rel="index" href="http://dilib.inist.fr/dilib/v04Main/IndexSite/Server/FR.resource.index.html"><link rel="top" title="Accueil de DILIB" href="http://dilib.inist.fr/dilib/v04Main/"><link rel="alternate" title="English version" href="paramShell.eng.html"><link rel="up" href="index.fre.html" title="Tutorial UNIX / Shell">
    <link rel="section" href="shellVars.fre.html" title="Variables d'environnement"><link rel="section" href="shellScript.fre.html" title="Scripts"><link rel="section" href="paramShell.fre.html" title="Param&egrave;tres"><link rel="section" href="testShell.fre.html" title="Tests"><link rel="section" href="structureShell.fre.html" title="Structures de contr&ocirc;le"><link rel="section" href="commandeShell.fre.html" title="Commandes diverses"><link rel="section" href="tpShell.fre.html" title="Travaux Pratiques">
    <link rel="first" href="caracteristiques.fre.html" title="Caract&eacute;ristiques d'UNIX">
    <link rel="previous" href="shellScript.fre.html" title="Scripts">
    <link rel="next" href="testShell.fre.html" title="Tests">
    <link rel="last" href="tpShell.fre.html" title="Travaux Pratiques">
  </head>

  <body>
    <h1>Param&egrave;tres - Shell - UNIX</h1>


    <div id="navcontainer">
      <ul id="navlist">
	<li><a href="caracteristiques.fre.html">UNIX</a>
	</li>
	<li><a href="redirPipes.fre.html">Bases</a>
	</li>
	<li><a href="newCommandsUser.fre.html">Commandes</a>
	</li>
	<li><a>Shell</a>
	  <ul id="subnavlist">
	    <li><a href="shellVars.fre.html">Variables d'environnement</a></li>
	    <li><a href="shellScript.fre.html">Scripts</a></li>
	    <li><a href="paramShell.fre.html" id="subcurrent">Param&egrave;tres</a></li>
	    <li><a href="testShell.fre.html">Tests</a></li>
	    <li><a href="structureShell.fre.html">Structures de contr&ocirc;le</a></li>
	    <li><a href="commandeShell.fre.html">Commandes</a></li>
	    <li><a href="tpShell.fre.html">TP</a></li>
	  </ul>
	</li>
	<li><a href="sed.fre.html">Sed</a>
	</li>
	<li><a href="make.fre.html">Make</a>
	</li>
      </ul>
    </div>
    
    <div class="content">
    <h2><a name="param"></a>
      R&eacute;cup&eacute;ration des param&egrave;tres de la ligne de commande
    </h2>
    <table border="1" class="var">
      <tbody>
	<tr>
	  
	  <th>$0</th>
	  <td>nom de la commande</td>
	</tr>
	<tr>
	  
	  <th>$n</th>
	  <td>valeur du n<sup>i&egrave;me</sup> param&egrave;tre</td>
	</tr>
	<tr>
	  
	  <th>$#</th>
	  <td>nombre de param&egrave;tres</td>
	</tr>
	<tr>
	  
	  <th>$*</th>
	  <td>liste de tous les param&egrave;tres</td>
	</tr>
      </tbody>
    </table>
    <p>
      Pour la ligne de commande <code>sh test.sh a b c</code>,
      <code>$0</code> vaut <code>test.sh</code>, <code>$#</code> vaut
      <code>3</code>, <code>$*</code> vaut <code>a b c</code>, et
      <code>$2</code> vaut <code>b</code> (&agrave; l'int&eacute;rieur du script).
    </p>
    <p>
      La commande <code>shift</code> d&eacute;cale les
      param&egrave;tres (en perdant le premier)&nbsp;: le
      deuxi&egrave;me param&egrave;tre devient le premier, le
      troisi&egrave;me devient le deuxi&egrave;me, etc. (c'est utile
      quand on utilise une boucle pour traiter les param&egrave;tres).
    </p>

    <h2><a name="speciales"></a>
      Variables sp&eacute;ciales
    </h2>
    <table border="1" class="var">
      <tbody>
	<tr>
	  
	  <th>$$</th>
	  <td>num&eacute;ro de processus de la derni&egrave;re commande</td>
	</tr>
	<tr>
	  
	  <th>$?</th>
	  <td>statut de la derni&egrave;re commande (0 = tout s'est bien pass&eacute;)</td>
	</tr>
      </tbody>
    </table>
    <h2><a name="speciaux"></a>
      Caract&egrave;res sp&eacute;ciaux
    </h2>
    <p>
      Comme &agrave; chaque fois qu'on a un caract&egrave;re
      sp&eacute;cial (par exemple <code>$</code>), on a des
      caract&egrave;res d'&eacute;chappement.
    </p>
    <table border="1" class="var">
      <tbody>
	<tr>
	  
	  <th>\</th>
	  <td>banalise le caract&egrave;re suivant</td>
	  <th>echo \$0 $0</th>
	  <td>$0 /usr/bin/ksh</td>
	</tr>
	<tr>
	  
	  <th>" ... "</th>
	  <td>banalise les caract&egrave;res sauf <code>\</code>, <code>$</code> et <code>`</code></td>
	  <th>echo "\$0 $0"</th>
	  <td>$0 /usr/bin/ksh</td>
	</tr>
	<tr>
	  
	  <th>' ... '</th>
	  <td>banalise tous les caract&egrave;res</td>
	  <th>echo '\$0 $0'</th>
	  <td>\$0 $0</td>
	</tr>
	<tr>
	  
	  <th>` ... `</th>
	  <td>substitution de commande</td>
	  <th>echo `date`</th>
	  <td>Mon May 5 16:54:14 MEST 2003</td>
	</tr>
      </tbody>
    </table>
    </div>
    <hr>
    <address><a href="mailto:philippe.houdry@inist.fr">Philippe.Houdry@inist.fr</a></address>
  </body>
</html>