LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-14-2011, 09:52 AM   #1
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 89

Rep: Reputation: 16
Question Why does echo -e "\\\\ " print only one \


Why does echo -e "\\\\ " print only one \?
echo -e "\\\\\ " prints two.
 
Old 09-14-2011, 11:03 AM   #2
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,429

Rep: Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348
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.
 
1 members found this post helpful.
Old 09-15-2011, 11:10 AM   #3
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 89

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by allend View Post
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.
 
Old 09-15-2011, 06:02 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
1 members found this post helpful.
Old 09-16-2011, 08:51 AM   #5
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 89

Original Poster
Rep: Reputation: 16
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using echo in some way to print the "hello" infinite times. viccimehta Linux - Newbie 1 04-11-2011 02:29 AM
[SOLVED] Perl: Condition?print "Is true":print "Is false"; angel115 Programming 2 09-17-2010 07:28 AM
printing hh in hh:mm using "awk '{FS=":";print $1}'" misses first line of output!! mayankmehta83 Linux - Newbie 2 12-03-2009 03:55 AM
upgrade to kernel 2.6.16.1 : "make bzImage" print "parse error" math_physics Red Hat 2 06-30-2007 12:04 AM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 03:14 PM


All times are GMT -5. The time now is 02:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration