Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Understanding escapes in echo
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  12 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Foo  
View profile  
 More options Feb 8, 10:20 am
Newsgroups: comp.unix.shell
From: Foo <no-such-u...@gmail.com>
Date: Wed, 8 Feb 2012 15:20:39 +0000 (UTC)
Local: Wed, Feb 8 2012 10:20 am
Subject: Understanding escapes in echo
Hi,

Can anyone help me to understand the following behavior of echo?

bash-4.2$ echo \\n
\n
bash-4.2$ echo \\\n
\n
bash-4.2$ echo `echo \\\n`
\n
bash-4.2$ echo `echo \\n`
n


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Laurianne Gardeux  
View profile  
 More options Feb 8, 12:00 pm
Newsgroups: comp.unix.shell
From: Laurianne Gardeux <l...@romandie.com>
Date: Wed, 8 Feb 2012 17:00:48 +0000 (UTC)
Local: Wed, Feb 8 2012 12:00 pm
Subject: Re: Understanding escapes in echo
Foo:

> Can anyone help me to understand the following behavior of echo?

> bash-4.2$ echo \\n
> \n
> bash-4.2$ echo \\\n
> \n
> bash-4.2$ echo `echo \\\n`
> \n
> bash-4.2$ echo `echo \\n`
> n

Not an explication, but a variation more:

bash-4.2$ echo $(echo \\n)
\n


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Gibson  
View profile  
 More options Feb 9, 1:35 pm
Newsgroups: comp.unix.shell
From: dave.gma+news...@googlemail.com.invalid (Dave Gibson)
Date: Thu, 09 Feb 2012 18:35:46 +0000
Local: Thurs, Feb 9 2012 1:35 pm
Subject: Re: Understanding escapes in echo

Foo <no-such-u...@gmail.com> wrote:
> Hi,

> Can anyone help me to understand the following behavior of echo?

> bash-4.2$ echo \\n
> \n
> bash-4.2$ echo \\\n
> \n
> bash-4.2$ echo `echo \\\n`
> \n
> bash-4.2$ echo `echo \\n`
> n

It's the shell (and the subshell in the latter two examples).  Some of
the backslashes are being processed as quoting the next character.  The
remainder are being treated literally by echo.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ed Morton  
View profile  
 More options Feb 9, 2:58 pm
Newsgroups: comp.unix.shell
From: "Ed Morton" <mortons...@gmail.com>
Date: Thu, 09 Feb 2012 19:58:37 GMT
Local: Thurs, Feb 9 2012 2:58 pm
Subject: Re: Understanding escapes in echo

Foo <no-such-u...@gmail.com> wrote:
> Hi,

> Can anyone help me to understand the following behavior of echo?

> bash-4.2$ echo \\n
> \n
> bash-4.2$ echo \\\n
> \n
> bash-4.2$ echo `echo \\\n`
> \n
> bash-4.2$ echo `echo \\n`
> n

Maybe but in reality you'd never use echo that way so it's not worth
thinking about. Let's see how this looks if you quote your expressions:

$ echo "\\n"
\n
$ echo "\\\n"
\\n
$ echo "$(echo "\\\n")"
\\n
$ echo "$(echo "\\n")"
\n

and if you follow the advice that's regularly given here and stop using
echo in favor of printf:

$ printf "%s\n" "\\n"
\n
$ printf "%s\n" "\\\n"
\\n
$ printf "%s\n" "$(printf "%s\n" "\\\n")"
\\n
$ printf "%s\n" "$(printf "%s\n" "\\n")"
\n

Looks to me like "\\" is simply an escaped backslash in the literal string
passed to printf while \n is a backslash then the letter n.

Interestingly, this is the first time I've actually come across a
difference between using `...` and $(...):

$ printf "%s\n"  `printf "%s\n" "\\\n"`
\n
$ printf "%s\n" $(printf "%s\n" "\\\n")
\\n

Regards,

   Ed.

Posted using www.webuse.net


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Foo  
View profile  
 More options Feb 9, 11:36 pm
Newsgroups: comp.unix.shell
From: Foo <no-such-u...@gmail.com>
Date: Fri, 10 Feb 2012 04:36:40 +0000 (UTC)
Local: Thurs, Feb 9 2012 11:36 pm
Subject: Re: Understanding escapes in echo

I am wondering why case 1 and case 2 give the same output but when
they are put into subshell the output is different?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Foo  
View profile  
 More options Feb 9, 11:44 pm
Newsgroups: comp.unix.shell
From: Foo <no-such-u...@gmail.com>
Date: Fri, 10 Feb 2012 04:44:40 +0000 (UTC)
Local: Thurs, Feb 9 2012 11:44 pm
Subject: Re: Understanding escapes in echo

Yes, it is artificial.

Yeah, I am wondering why the last part "\n" is not treated as newline...

> Interestingly, this is the first time I've actually come across a
> difference between using `...` and $(...):

> $ printf "%s\n"  `printf "%s\n" "\\\n"`
> \n
> $ printf "%s\n" $(printf "%s\n" "\\\n")
> \\n

Me too!!

Could anyone here give an explanation?

Thanks!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Gibson  
View profile  
 More options Feb 10, 7:02 am
Newsgroups: comp.unix.shell
From: dave.gma+news...@googlemail.com.invalid (Dave Gibson)
Date: Fri, 10 Feb 2012 12:02:03 +0000
Local: Fri, Feb 10 2012 7:02 am
Subject: Re: Understanding escapes in echo

Unquoted backslashes remove special meaning from the following
character (with an exception for a backslash+newline pair).

In the first example the first backslash quotes the second.

  echo \\n
       \\       -> \
         n      -> n
  echo \n        <- final command

In the second example the first backslash quotes the second and the
third backslash quotes the 'n'

  echo \\\n
       \\       -> \
         \n     -> n
  echo \n        <- final command

> but when
> they are put into subshell the output is different?

In the third and fourth examples the backslashes are being processed
twice -- once by the parent shell (outside the `...`) and again by the
subshell (inside the `...`).

When processing a back-quoted command substitution the parent shell
will only treat a backslash as escaping the next character if the next
character is a back-quote, a backslash or a dollar sign.  If the
command substitution is within double quotes a backslash also escapes
a double quote.

In the third example the parent shell treats the first backslash as
quoting the second.  As the third backslash is not followed by one
of [`\$] it is treated as a literal character.  The subshell then
processes the command within back-quotes using unquoted backslash
rules.

  echo `echo \\\n`
             \\     -> \           }
               \    -> \           } parent processing
                n   -> n           }
        echo \\n     <- subshell command
             \\     -> \           } subshell processing
               n    -> n           }
        echo \n      <- command executed by subshell
       \n            <- captured output
  echo \n            <- final command

In the fourth example the parent shell treats the first backslash as
escaping the second.  The subshell then processes the backslashes and
treats the '\' as escaping the 'n'.

  echo `echo \\n`
             \\     -> \           } parent processing
               n    -> n           }
        echo \n      <- subshell command
             \n     -> n           } subshell processing
        echo n       <- command executed by subshell
       n             <- captured output
  echo n             <- final command

You may be expecting your shell's echo to behave like echo -e.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ed Morton  
View profile  
 More options Feb 10, 7:31 am
Newsgroups: comp.unix.shell
From: Ed Morton <mortons...@gmail.com>
Date: Fri, 10 Feb 2012 06:31:50 -0600
Local: Fri, Feb 10 2012 7:31 am
Subject: Re: Understanding escapes in echo
On 2/9/2012 10:44 PM, Foo wrote:

printf: Because it's in the string to be printed, not in the format string.

echo: because your particular echo doesn't interpret escape sequences by
default. Try it with "-e":

$ echo "a\nb"
a\nb
$ echo -e "a\nb"
a
b

That inconsistency between echos is one reason people advise using printf.

     Ed.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bsh  
View profile  
 More options Feb 14, 5:56 pm
Newsgroups: comp.unix.shell
From: bsh <brian_hi...@rocketmail.com>
Date: Tue, 14 Feb 2012 14:56:52 -0800 (PST)
Local: Tues, Feb 14 2012 5:56 pm
Subject: Re: Understanding escapes in echo
On Feb 8, 7:20 am, Foo <no-such-u...@gmail.com> wrote:

> Can anyone help me to understand the following behavior of echo?
> bash-4.2$ echo \\n
> \n
> bash-4.2$ echo \\\n
> \n
> bash-4.2$ echo `echo \\\n`
> \n
> bash-4.2$ echo `echo \\n`
> n

I just recently posted a response that is pertainent to this issue,
ostensively dealing with options parsing, but with a script example
and addendum that expands upon your issue:

http://groups.google.com/group/comp.unix.shell/browse_thread/thread/c...

As for "shell quoting", the only thing that you have to know now, is
that shell, quote parsing, and "word separation" really are internally
consistent, and when (with more scripting experience) you understand
how the shell (an interpreter) differs in the parsing of its code from
a
compiler's usual approach, it will eventually become understandable to
you. In the meantime, some documents to help you on your way are:

"UNIX SHELL Quote Tutorial"
http://www.grymoire.com/Unix/Quote.html

"Consultix Shell Quoting Guidelines"
http://www.consultix-inc.com/quoting1_2.txt

"A Guide to Unix Shell Quoting"
http://www.mpi-inf.mpg.de/~uwe/lehre/unixffb/quoting-guide.html

"The Open Group Base Specifications (IEEE Std 1003.1) -- 2.6 Word
Expansions"
http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.ht...

Read the above in the order they are listed. Good luck!

=Brian


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Foo  
View profile  
 More options Feb 15, 7:43 am
Newsgroups: comp.unix.shell
From: Foo <no-such-u...@gmail.com>
Date: Wed, 15 Feb 2012 12:43:05 +0000 (UTC)
Local: Wed, Feb 15 2012 7:43 am
Subject: Re: Understanding escapes in echo
Excellent answer! This is exactly what I want!

Thanks, Dave!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ivan Shmakov  
View profile  
 More options Feb 16, 12:03 pm
Newsgroups: comp.unix.shell
From: Ivan Shmakov <oneing...@gmail.com>
Date: Fri, 17 Feb 2012 00:03:46 +0700
Local: Thurs, Feb 16 2012 12:03 pm
Subject: Re: Understanding escapes in echo

>>>>> Dave Gibson <dave.gma> writes:
>>>>> Foo <no-such-u...@gmail.com> wrote:
>>>>> On Thu, 09 Feb 2012 at 18:35 GMT, Dave Gibson wrote:
>>>>> Foo <no-such-u...@gmail.com> wrote:

[...]

 >>>> bash-4.2$ echo `echo \\\n`
 >>>> \n
 >>>> bash-4.2$ echo `echo \\n`
 >>>> n

 >>> It's the shell (and the subshell in the latter two examples).  Some
 >>> of the backslashes are being processed as quoting the next
 >>> character.  The remainder are being treated literally by echo.

[...]

 >> but when they are put into subshell the output is different?

 > In the third and fourth examples the backslashes are being processed
 > twice -- once by the parent shell (outside the `...`) and again by
 > the subshell (inside the `...`).

        ... And that's why they prefer $() over `` nowadays.  Consider,
        e. g.:

$ echo $(echo \\\n)
\n
$ echo $(echo  \\n)
\n
$

        These also nest neatly, like:

$ echo $(echo $(echo $(echo $(echo $(echo $(echo  \\n))))))
\n
$

[...]

--
FSF associate member #7257


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bsh  
View profile  
 More options Feb 16, 7:57 pm
Newsgroups: comp.unix.shell
From: bsh <brian_hi...@rocketmail.com>
Date: Thu, 16 Feb 2012 16:57:30 -0800 (PST)
Local: Thurs, Feb 16 2012 7:57 pm
Subject: Re: Understanding escapes in echo

bsh <brian_hi...@rocketmail.com> wrote:
> Foo <no-such-u...@gmail.com> wrote:
> > ...
> ...

In addition to the links already provided, let me add another one just
discovered and appreciated of Sven Mascheck's great resource
regarding shell versions, portability concerns, and their host OSes,
in regard to my most recent posting:

"echo(1) And printf(1)"
http://www.in-ulm.de/~mascheck/various/echo+printf/

=Brian


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »