×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

There are two layers of escape you need to consider: shell itself, and echo program

本文发表在 rolia.net 枫下论坛In unix, when you invoke a command under shell, basically the current shell will take your input, and fork() a new process, then exec() your command on top of that process, along with the processed arguments and old shell's enviornment. The shell itself will process the command line according to its own rule, in bash, it will consider "\" as an escape character and take the literal meaning of the character that follows. So,
if you put "\\" in shell, shell will convert it to a single "\" , then pass it to exec() function call. In this case, if you write "echo \\\\\\\\" in shell, the shell will change it to "echo \\\\" (it takes half of the "\" off) and pass this to exec(). When exec() receives its argument of "\\\\", it in turn does its own processing, which also considers "\" as an escape character and thus reduces the "\\\\" to "\\", so, you end up with the output of "\\". Echo has a
peculiar behavior that when it encounters a single "\" who doesn't have anybody else to escape, it will print out "\" anyway. This is why you end up with "\\" too when you input "\\\\\\". This strange escaping behaviour of shell has caused a lot of confusion for reqular expressions, where you need to put double slash before a number in order to represent a previously encoutered RE, such as "\\0"更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / Sailor和Egg,看你们“啊”得这么起劲,来回答我的问题:为什么echo \\和echo \\\\的结果都是\,echo \\\\\\和echo \\\\\\\\的结果都是\\,而echo \\\\\\\\\\和echo \\\\\\\\\\\\的结果都是\\\呢?
    • Is it from Unix/Linux? Don't tell me it is from Dos/Windows.
      • Bourne Shell
    • numnum? any body?
    • 后边的那些都被忽略了
      • Detail! From which?
    • 只有idiot才研究这种问题。in shell, \\ = \? sorry, I'm a idiot on UNIX.
    • Explain again: echo 2 \ and 4 \ get 1 \; echo 6 \ and 8 \ get 2 \; echo 10 \ and 12 \ get 3 \. Why?
      • The backslash is intepreted by shell. \\=\. I got 4 \ when I put 8 \ on tcsh. I am glad to talk about it further with you.
        • Thx! I know \\ = \. But I was really puzzled by so many \'s lined up.
    • don't know,,, you are a weird guy, always studying weird problems. hehe
      • Help me. At least explain why echo \\ and echo \\\\ are both producing \?
        • okay, I already know why, any prize?
          • I will go to watch how do you put the ring on lumlum's finger. xixi.
            • be patient, I am writing the answer, it's a bit complicated to explain all this
              • NP&THX
            • me too.
              • be patient. he's working for the answer. it's a bit complicated to work out the whole thing. hehe.
                • 不行啦,我肚子笑得要抽筋啦……
                  • Why do you laugh at a guy he is really serious about romance? Do not laugh, just be patient.
                    • ?
          • There are two layers of escape you need to consider: shell itself, and echo program
            本文发表在 rolia.net 枫下论坛In unix, when you invoke a command under shell, basically the current shell will take your input, and fork() a new process, then exec() your command on top of that process, along with the processed arguments and old shell's enviornment. The shell itself will process the command line according to its own rule, in bash, it will consider "\" as an escape character and take the literal meaning of the character that follows. So,
            if you put "\\" in shell, shell will convert it to a single "\" , then pass it to exec() function call. In this case, if you write "echo \\\\\\\\" in shell, the shell will change it to "echo \\\\" (it takes half of the "\" off) and pass this to exec(). When exec() receives its argument of "\\\\", it in turn does its own processing, which also considers "\" as an escape character and thus reduces the "\\\\" to "\\", so, you end up with the output of "\\". Echo has a
            peculiar behavior that when it encounters a single "\" who doesn't have anybody else to escape, it will print out "\" anyway. This is why you end up with "\\" too when you input "\\\\\\". This strange escaping behaviour of shell has caused a lot of confusion for reqular expressions, where you need to put double slash before a number in order to represent a previously encoutered RE, such as "\\0"更多精彩文章及讨论,请光临枫下论坛 rolia.net
            • THX! The key to me is :"Echo has a peculiar behavior that when it encounters a single "\" who doesn't have anybody else to escape, it will print out "\" anyway. " I know the rest. And I did not ask this question for fun.
              Cuz I wrote below statement in my script:

              sed s/\\\\//\\\\\\\\\\\\//g tmpfile

              And I was really puzzled when I tune this script. I had to try many times to get the correct form.

              hehe...What's more, do you know what did I want to do with the statement above?
              • 嘿嘿,你整天就搞些怪东西, 你用nawk应该会好多了
                • awk要调解释器,麻烦,而且运行级别低的时候不能用。而且个人认为awk的长处是做运算和有比较复杂流程的任务,比方说处理log之类的;文本处理还是用ed、sed和tr比较爽。虽然regexp比较晦涩,但是其实很好用;喜欢摆弄它就象有的人喜欢开手动档的汽车一样。
                  不过可惜还没玩熟,而且还老容易忘。
                  • exactly.
                  • awk is almost as simple as sed, you can put the script directly in the command line. It has a much more complete set of utilities, sometimes it is viewed as a programming language, like perl.
                    Actully, I only use sed as "sed -n "s/***/88/g" " because it already beomes a habit.
                    • Yes, awk is a script language indeed. It's the very reason I don't want to use it to deal with the text. You know, you will have to almost re-write a part of sed, ed or tr. Why not use a provided utillity?
                • BTW, How did you know the stupid feature of echo? I cannot get it from man page. Is there any document for it?
                  • 感觉啦,如果只有一个”\”,他ESCAPE谁去啊?但你不能就这么忽视它巴,这些东西很多都是COMMON的。 后来写程序时候, 用execlp()把ECHO绕过SHELL调用了一下,证明是这样。 好了,要上班去啦。。。呵呵,等会再聊
                    • 我还以为你看过source code呢,呵呵。Thx!
                      • heihei, I'm in the office now, just got a cup of coffee. Echo's source code should be really simple, I have written some kind of regular expression library for real time system, it's common sense how to interpret certain things.
                        • Obviously it's common to you programmers. hehe...