{"id":31173,"date":"2006-05-17T10:00:00","date_gmt":"2006-05-17T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2006\/05\/17\/beware-of-digits-before-the-redirection-operator\/"},"modified":"2006-05-17T10:00:00","modified_gmt":"2006-05-17T10:00:00","slug":"beware-of-digits-before-the-redirection-operator","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20060517-00\/?p=31173","title":{"rendered":"Beware of digits before the redirection operator"},"content":{"rendered":"<p>\nIf you want to put the string &#8220;Meet at 2&#8221; into the file &#8220;schedule&#8221;,\nyou might be tempted to use\n<\/p>\n<pre>\necho Meet at 2&gt;schedule\n<\/pre>\n<p>\nIf you try this, however, you&#8217;ll see the string &#8220;Meet at&#8221; on the\nscreen and the &#8220;schedule&#8221; file will be blank.\n[Typo fixed, 10am]\n<\/p>\n<p>\nWhat happened?\n<\/p>\n<p>\nA digit immediately before a redirection operator modifies\nwhich stream the redirection operator applies to.\nIf you&#8217;re going to redirected an alternate output stream,\nit&#8217;ll nearly always be the standard error stream, or stream&nbsp;2.\nTo put the error output into a file, you would write\nsomething like this:\n<\/p>\n<pre>\nsort \/invalidswitch 2&gt;errorfile\n<\/pre>\n<p>\nThere is also the operator &#8220;&gt;&amp;&#8221; that reopens a stream\nas another stream.\nThe idiom\n<\/p>\n<pre>\nsome-command &gt;output 2&gt;&amp;1\n<\/pre>\n<p>\nsays, &#8220;Put the normal output into the file <code>output<\/code>,\nand then change the error output stream (2) to refer to the\nnormal output stream (1).&#8221;\nThe result is that both the regular output and error output\nend up in the <code>output<\/code> file.\n<\/p>\n<p>\nBut what if you really want to put the string &#8220;Meet at 2&#8221; into\nthe file &#8220;schedule&#8221;?\n<\/p>\n<p>\nYou can insert a space between the &#8220;2&#8221; and the &#8220;&gt;&#8221;.\nThis works for most programs since they ignore trailing spaces\non their command line, but this was a trick question:\nThe <code>echo<\/code> command is one of the few commands\nthat actually pays attention to trailing spaces.\nAs a result, the contents of the &#8220;schedule&#8221; file is\n&#8220;Meet at 2&lt;space&gt;&lt;cr&gt;&lt;lf&gt;&#8221;.\nMaybe this is close enough for you, in which case you can skip\nthe next paragraph.\n<\/p>\n<p>\nBut what if you don&#8217;t want that trailing space?\nFor that, you can use the metacharacter escape character, the <tt>^<\/tt>:\n<\/p>\n<pre>\necho Meet at ^2&gt;schedule\n<\/pre>\n<p>\nThe last gotcha is that the pesky &#8220;2&#8221; might come from environment\nvariable expansion.\n<\/p>\n<pre>\nset message=Meet at 2\necho %message%&gt;schedule\n<\/pre>\n<p>\nThe trailing &#8220;2&#8221; in <code>%message%<\/code> interacts with the\ngreater-than sign, leading to an unintended redirection.\nFor this, you can insert a space before the greater-than sign,\nassuming you are in a scenario where that space is not going to\ncause you any problems.\n(And if you&#8217;re in a scenario where that space will cause a problem,\nyou can use a trick we&#8217;ll look at next time.)\n<\/p>\n<p>\nMind you, if you&#8217;re going to take an environment variable\nwhose contents you do not control and expand it\nonto your command line unquoted, you have much worse problems\nthan a trailing digit messing up your file redirection.\nSomebody might have decided that the message\nshould be &#8220;<code>&amp;format C: \/y<\/code>&#8220;.  Inserting this into the command\nline unquoted would yield &#8220;<code>echo &amp;format C: \/y&gt;schedule<\/code>&#8221;\nwhich is a pretty good way to ruin somebody&#8217;s day.\n(Well, okay, you can&#8217;t format a drive with an active pagefile,\nbut you get the idea.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to put the string &#8220;Meet at 2&#8221; into the file &#8220;schedule&#8221;, you might be tempted to use echo Meet at 2&gt;schedule If you try this, however, you&#8217;ll see the string &#8220;Meet at&#8221; on the screen and the &#8220;schedule&#8221; file will be blank. [Typo fixed, 10am] What happened? A digit immediately before a [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[104],"class_list":["post-31173","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-tipssupport"],"acf":[],"blog_post_summary":"<p>If you want to put the string &#8220;Meet at 2&#8221; into the file &#8220;schedule&#8221;, you might be tempted to use echo Meet at 2&gt;schedule If you try this, however, you&#8217;ll see the string &#8220;Meet at&#8221; on the screen and the &#8220;schedule&#8221; file will be blank. [Typo fixed, 10am] What happened? A digit immediately before a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/31173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=31173"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/31173\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=31173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=31173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=31173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}