LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Why does echo -e "\\\\ " print only one \ (https://www.linuxquestions.org/questions/linux-newbie-8/why-does-echo-e-%5C%5C%5C%5C-print-only-one-%5C-902984/)

doru 09-14-2011 08:52 AM

Why does echo -e "\\\\ " print only one \
 
Why does echo -e "\\\\ " print only one \?
echo -e "\\\\\ " prints two.

allend 09-14-2011 10:03 AM

From 'man bash'
Quote:

QUOTING ...
Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \,
...
So the commands run by the shell are 'echo -e "\\ " ' and 'echo -e "\\\ " ' respectively.
As the -e option actually interprets backslash escapes, the output from the first command is a single backslash. The second command reads the first and second backslashes and outputs a single blackslash, then reads the second and third backslashes and outputs the second blackslash.

doru 09-15-2011 10:10 AM

Quote:

Originally Posted by allend (Post 4471520)
From 'man bash'

So the commands run by the shell are 'echo -e "\\ " ' and 'echo -e "\\\ " ' respectively.
As the -e option actually interprets backslash escapes, the output from the first command is a single backslash. The second command reads the first and second backslashes and outputs a single blackslash, then reads the second and third backslashes and outputs the second blackslash.

Thank you!

So you say that:
echo -e "\\\\ " --bash--> echo -e "\\ " --echo -e--> "\ "
echo -e "\\\\\ " --bash--> echo -e "\\\ " --echo -e--> "\\ "
echo -e "\\\\\\ " --bash--> echo -e "\\\ " --echo -e--> "\\ "
In the second line, why would bash preserve the "hanging" last \?
In the second and third lines, why would echo -e print the third "hanging" \?
They do, as you say, but where is this written in manuals? "" are not really there after bash.

David the H. 09-15-2011 05:02 PM

Please use [code][/code] tags around your code, to preserve formatting and to improve readability. It would help to see what's going on here.

According to the bash man page:
Code:

QUOTING

        ...

        Enclosing characters in double quotes preserves the literal value of
        all characters within the quotes, with the exception of $, `, \, and,
        when history expansion is enabled, !.  The characters $ and ` retain
        their special meaning within double quotes.  The backslash retains its
        special meaning only when followed by one of the following characters:
        $, `, ", \, or <newline>.  A double quote may be quoted within double
        quotes by preceding it with a backslash.  If enabled, history expansion
        will be performed unless an ! appearing in double quotes is escaped using
        a backslash.  The backslash preceding the ! is not removed.

...and...

Code:

echo [-neE] [arg ...]
        Output the args, separated by spaces, followed by a newline.  The
        return status is always 0.  If -n is specified, the trailing
        newline is suppressed.  If the -e option is given, interpretation
        of the following backslash-escaped characters is enabled.  The -E
        option disables the interpretation of these escape characters, even
        on systems where they are interpreted by default.  The xpg_echo        shell
        option may be used to dynamically determine whether or not echo
        expands these escape characters by default. echo does not interpret --
        to mean the end of options.  echo interprets the following escape
        sequences:

              \a    alert (bell)
              \b    backspace
              \c    suppress further output
              \e    an escape character
              \f    form feed
              \n    new line
              \r    carriage return
              \t    horizontal tab
              \v    vertical tab
              \\    backslash
              \0nnn  the eight-bit character whose value is the
                    octal value nnn (zero to three octal digits)
              \xHH  the eight-bit character whose value is the
                    hexadecimal value HH (one or two hex digits)

So it seems that when inside double-quotes, the shell only reduces \\, \$, \`, \", and \<newline>. All other backslash combinations are passed as is. Then echo -e similarly processes only the patterns listed above.

In all of your lines above, the last character on the line is a space, so what you're really seeing is a literal "\ " combination. Try replacing the space with some other character, and/or bracket the results, to see it more clearly. Also, use the plain echo command to see what the shell is really executing before the -e interpretation further reduces it.
Code:

$ echo "[\\\\S]"
[\\S]
$ echo -e "[\\\\S]"
[\S]

$ echo "[\\\\\S]"
[\\\S]
$ echo -e "[\\\\\S]"
[\\S]

$ echo "[\\\\\\S]"
[\\\S]
$ echo -e "[\\\\\\S]"
[\\S]

Finally, don't forget that single quotes protect all characters from the shell, so you can use them instead to feed echo the literal string you want to print, rather than trying to figure out how many backslashes to use.

doru 09-16-2011 07:51 AM

Thank you a lot!!
Quote:

Enclosing characters in double quotes [...]. The backslash retains its special meaning only when followed by one of the following characters: $, ‘, ", \, or <newline>.
So when an odd number of \'s is given, followed by a blank, the last hanging one is treated as a normal character.

Then, echo -e does the same thing again and this generated those strange results.

I take your advices.
Thanks again.


All times are GMT -5. The time now is 05:55 PM.