{"id":96725,"date":"2017-08-01T07:00:00","date_gmt":"2017-08-01T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=96725"},"modified":"2019-03-13T01:14:43","modified_gmt":"2019-03-13T08:14:43","slug":"20170801-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170801-00\/?p=96725","title":{"rendered":"The redirection can come anywhere on the line, and you can use that to get rid of the spaces"},"content":{"rendered":"<p>We saw last time that <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20170731-00\/?p=96715\">the redirection can come anywhere on the line<\/a>, and we saw that unquoted paths with embedded spaces can seem to work, but in fact doesn&#8217;t. And you don&#8217;t notice because the command processor is not parsing the command the way you think. <\/p>\n<p>Another problem with redirection is the unwanted trailing spaces. If you say <\/p>\n<pre>\necho foo &gt;result.txt\n<\/pre>\n<p>then the file <code>result.txt<\/code> consists of six characters: <\/p>\n<table BORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"3\">\n<tr>\n<td><code>f<\/code><\/td>\n<td><code>o<\/code><\/td>\n<td><code>o<\/code><\/td>\n<td><code>&nbsp;<\/code><\/td>\n<td><code>\\r<\/code><\/td>\n<td><code>\\n<\/code><\/td>\n<\/tr>\n<\/table>\n<p>Notice that there&#8217;s a space before the <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20040318-00\/?p=40193\">CR+LF<\/a>. You probably didn&#8217;t want that space, but it&#8217;s there because you put a space before the redirection operator. The parser takes out the redirection operator and the file name, but the space before the redirection operator is still there. <\/p>\n<p>You can remove the space by squishing the redirection operator right against the string you want to print. <\/p>\n<pre>\necho foo&gt;result.txt\n<\/pre>\n<p>Now the resulting file is <\/p>\n<table BORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"3\">\n<tr>\n<td><code>f<\/code><\/td>\n<td><code>o<\/code><\/td>\n<td><code>o<\/code><\/td>\n<td><code>\\r<\/code><\/td>\n<td><code>\\n<\/code><\/td>\n<\/tr>\n<\/table>\n<p>Cool. No stray space. <\/p>\n<p>And then later you get a bug because this string didn&#8217;t get saved to the file properly: <\/p>\n<pre>\nset MESSAGE=2 for 1\necho %MESSAGE%&gt;result.txt\n<\/pre>\n<p>The resulting file is <\/p>\n<table BORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"3\">\n<tr>\n<td><code>2<\/code><\/td>\n<td><code>&nbsp;<\/code><\/td>\n<td><code>f<\/code><\/td>\n<td><code>o<\/code><\/td>\n<td><code>r<\/code><\/td>\n<td><code>&nbsp;<\/code><\/td>\n<td><code>\\r<\/code><\/td>\n<td><code>\\n<\/code><\/td>\n<\/tr>\n<\/table>\n<p>Where did the <code>1<\/code> go? <\/p>\n<p>Another part of the syntax for redirection is that if you put a number in front of the redirection operator, it specifies which file descriptor you want to redirect. In practice, this number is nearly always 2 (stderr), because 1 (stdout) is the default, and 0 is stdin, which nobody writes to. But it means that the expansion of the command line becomes <\/p>\n<pre>\necho 2 for 1&gt;result.txt\n<\/pre>\n<p>and you are now performing an explicit redirection of stdout. The <code>1<\/code> is redundant because the default is stdout, but hey, you asked for it. <\/p>\n<p>To get rid of this space, you can take advantage of the principle in the subject of today&#8217;s post. Put the redirection operator somewhere else. I like to put it at the front. <\/p>\n<pre>\n&gt;result.txt echo %MESSAGE%\n<\/pre>\n<p>Putting the redirection at the front also makes building a multi-line document easier to read. <\/p>\n<pre>\nrem old way\necho first line&gt;result.txt\necho second line&gt;&gt;result.txt\necho third line&gt;&gt;result.txt\n\nrem new way\n &gt;result.txt echo first line\n&gt;&gt;result.txt echo second line\n&gt;&gt;result.txt echo third line\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Up front and personal.<\/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-96725","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Up front and personal.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96725","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=96725"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96725\/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=96725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=96725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=96725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}