Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 29/10/2015, à 12:03

DonutMan75

[RESOLU] [BASH/SH] Différence commande echo

Bonjour,

J'ai deux comportements différents de la fonction echo selon que j'utilise sh (#!/bin/sh) ou bash (#!/bin/bash)....
Voici le script :

== VERSION AVEC SH ==

Code :

#!/bin/sh
echo -e "toto"

Exécution :

$ ./test.sh 
-e toto

== VERSION AVEC BASH ==

Code :

#!/bin/bash
echo -e "toto"

Exécution :

$ ./test.sh 
toto

Je suppute que le comportement est différent car echo est une fonction interne au shell
Quand je fais un man echo dans mon terminal (qui tourne sous bash), l'option -e est bien présente.

Et pourtant....
Si je fais :

$ sh
> man echo

J'obtiens :

ECHO(1)                                                               User Commands                                                              ECHO(1)



NAME
       echo - display a line of text

SYNOPSIS
       echo [SHORT-OPTION]... [STRING]...
       echo LONG-OPTION

DESCRIPTION
       Echo the STRING(s) to standard output.

       -n     do not output the trailing newline

       -e     enable interpretation of backslash escapes

[...]

Je ne comprends donc pas pourquoi mon -e est interprété comme une entrée et non comme une option.......

Merci d'avance pour vos éclaircissements smile

D.

Dernière modification par DonutMan75 (Le 29/10/2015, à 17:16)

Hors ligne

#2 Le 29/10/2015, à 12:32

Postmortem

Re : [RESOLU] [BASH/SH] Différence commande echo

Salut,
Il me semble que lorsque tu fais "man echo", c'est la page de manuel de "/bin/echo" (ou il est situé ailleurs, je ne sais plus), bref, du programme externe "echo".
Pour voir les options du echo interne à sh, c'est dans "man sh".


Mot' a dit : « Un Hellfest sans Slayer, c'est comme une galette-saucisse sans saucisse ! »

Hors ligne

#3 Le 29/10/2015, à 13:35

MicP

Re : [RESOLU] [BASH/SH] Différence commande echo

Les scripts sh sont exécutés par l'interpréteur de commandes dash.

michel@xubu:~$ file $(which sh)
/bin/sh: symbolic link to `dash' 
michel@xubu:~$ 

Mais la commande interne echo de l'interpréteur de commandes dash n'a pas d'option e

man dash a écrit :


     echo [-n] args...
            Print the arguments on the standard output, separated by spaces.  Unless the -n option is present, a newline is output fol‐
            lowing the arguments.

            If any of the following sequences of characters is encountered during output, the sequence is not output.  Instead, the
            specified action is performed:

            \b      A backspace character is output.

            \c      Subsequent output is suppressed.  This is normally used at the end of the last argument to suppress the trailing
                    newline that echo would otherwise output.

            \f      Output a form feed.

            \n      Output a newline character.

            \r      Output a carriage return.

            \t      Output a (horizontal) tab character.

            \v      Output a vertical tab.

            \0digits
                    Output the character whose value is given by zero to three octal digits.  If there are zero digits, a nul character
                    is output.

            \\      Output a backslash.

            All other backslash sequences elicit undefined behaviour.

     eval string ...

Dernière modification par MicP (Le 29/10/2015, à 13:43)

Hors ligne

#4 Le 29/10/2015, à 15:08

ar barzh paour

Re : [RESOLU] [BASH/SH] Différence commande echo

autre une réponse possible ici :

bash
$ type echo
echo est une primitive du shell
$type man
man est /usr/bin/man
sh
$ type echo
echo is a shell builtin
$ type man
man is a tracked alias for /usr/bin/man

on voit que echo est bien une commande interne ( bash et sh)
mais que man pointe dans les deux cas sur /usr/bin/man


et je m'apercois aussi que help echo n'existe pas dans sh ?

@MicP : man dash c'est la façon d'avoir l'équivalent de help dans sh  ?

Dernière modification par ar barzh paour (Le 29/10/2015, à 15:20)


PC          : B760M DS3H DDR4,  12th Gen Intel(R) Core(TM) i3-12100, RAM DDR4 8GiB -2400 Ubuntu 22.04, 22.04, 23.04
Portable1 : Intel(R) Core(TM)2 Duo CPU     T6570  @ 2.10GHz RAM 4GiB DDR2 667 MHz Ubuntu 23.04 ( en voyage )
Portable2 : T5750  @ 2.00GHz RAM 1GiB DDR2 667 Mhz Ubuntu 20.04 ( batterie HS )
stourm a ran war bep tachenn (Angela Duval) ( Je combats sur tous les fronts )

Hors ligne

#5 Le 29/10/2015, à 16:41

MicP

Re : [RESOLU] [BASH/SH] Différence commande echo

Il y a 3 différentes commandes echo :

- L'exécutable /bin/echo
- La commande echo de l'interpréteur de commandes dash
- La commande echo de l'interpréteur de commandes bash

=======
La ligne de commande man dash m'a permis d'afficher la page du manuel de l'interpréteur de commandes dash
dont je n'ai transmis que l'extrait qui concernait sa commande interne echo

=======
La commande help est une commande interne de l'interpréteur de commande bash

Dernière modification par MicP (Le 29/10/2015, à 17:26)

Hors ligne

#6 Le 29/10/2015, à 17:16

DonutMan75

Re : [RESOLU] [BASH/SH] Différence commande echo

Bonjour à tous,
merci pour vos précisions !

En fait je pensais que 'man echo' affichait la description de l'écho correspondant au shell dans lequel on se trouvait... d'où mon erreur wink

Merci à tous,

D.

Hors ligne