{"id":104452,"date":"2020-11-16T07:00:00","date_gmt":"2020-11-16T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=104452"},"modified":"2020-11-15T18:33:17","modified_gmt":"2020-11-16T02:33:17","slug":"20201116-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20201116-00\/?p=104452","title":{"rendered":"Why does my command line redirection echo with an extra 1? Who&#8217;s inserting these rogue 1s everywhere?"},"content":{"rendered":"<p>If you leave <code>echo<\/code> enabled in your batch file, and the batch file executes a command with redirection, then a mysterious <code>1<\/code> is inserted.<\/p>\n<pre>C:\\&gt; copy con %TEMP%\\test.cmd\r\ndir &gt;nul\r\n^Z\r\n        1 file(s) copied.\r\n\r\nC:\\&gt; %TEMP%\\test.cmd\r\n\r\nC:\\&gt; dir  1&gt;nul\r\n          <span style=\"color: red;\">\u21d1<\/span>\r\n          \ud83e\udd28\r\n<\/pre>\n<p>The <a href=\"https:\/\/www.imdb.com\/title\/tt3748528\/\"> rogue <code>1<\/code><\/a> appears to be a purely visual glitch. There is no actual <code>1<\/code> inserted into the command line. In this example, the directory listing is of the current directory, not of a directory named <code>1<\/code>.<\/p>\n<p>Where is this <code>1<\/code> coming from?<\/p>\n<p>Recall that <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20060517-00\/?p=31173\"> digits before the redirection operator specify the stream being redirected<\/a>. (This is one of the reasons why I have a habit of putting <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170801-00\/?p=96725\"> the redirection at the start of the command line<\/a>.)<\/p>\n<p>By default, if there is no stream specified, then the stream that is redirected is <code>stdout<\/code>, and <code>stdout<\/code>&#8216;s stream number is\u2026 1.<\/p>\n<p>Okay, the pieces are starting to come together.<\/p>\n<p>If <code>echo<\/code> is enabled, then the command processor prints each command before executing it. But this happens <i>after<\/i> the command line is parsed, and part of parsing the redirection operator is remembering which stream is being redirected. The command processor regenerates the original command line from what it had parsed, and when it sees that the parsed redirection operator, it dutifully puts the stream number in front.<\/p>\n<p>If you didn&#8217;t specify an explicit stream to redirect, then the default stream (<code>stdout<\/code> = <code>1<\/code>) is printed, because that is in fact what stream is being redirected.<\/p>\n<p>It&#8217;s just a quirk.\u00a0The functionality is unaffected. Though it does look a little weird the first time you notice it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It just came out of nowhere, or did it?<\/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":[25],"class_list":["post-104452","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It just came out of nowhere, or did it?<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104452","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=104452"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104452\/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=104452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=104452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=104452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}