{"id":96735,"date":"2017-08-02T07:00:00","date_gmt":"2017-08-02T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=96735"},"modified":"2019-03-13T01:14:46","modified_gmt":"2019-03-13T08:14:46","slug":"20170802-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170802-00\/?p=96735","title":{"rendered":"Why does attempting to echo an undefined environment variable result in the message &#8220;ECHO is on&#8221;?"},"content":{"rendered":"<p>From a batch file, suppose you try to echo the contents of a variable that hasn&#8217;t been defined: <\/p>\n<pre>\nset AWESOME=1\necho %AWESUM%\n<\/pre>\n<p>This prints <code>ECHO is off<\/code>. What&#8217;s up with that? <\/p>\n<p>The first half of the puzzle is knowing that if a variable is not defined, then it expands to an empty string. Therefore, after expension, the command <\/p>\n<pre>\necho %AWESUM%\n<\/pre>\n<p>becomes simply <\/p>\n<pre>\necho\n<\/pre>\n<p>The second half of the puzzle is knowing that the <code>ECHO<\/code> command, if run with no command line, reports the current echo state, printing either <code>ECHO is on<\/code> or <code>ECHO is off<\/code>, accordingly. <\/p>\n<p>Putting it all together: If you try to echo the value of an undefined variable, and the variable is not defined, then the echo command gets a blank command line, at which point it reports its current state. <\/p>\n<p>So how do you echo the value of a variable that might be undefined? <\/p>\n<p>One way is to put something else on the command line. <\/p>\n<pre>\necho AWESUM=%AWESUM%\n<\/pre>\n<p>If <code>AWESUM<\/code> is not defined, this will expand to <\/p>\n<pre>\necho AWESUM=\n<\/pre>\n<p>and that prints <code>AWESUM=<\/code>. <\/p>\n<p>Okay, but what if you really want to echo the value on a line all by itself? <\/p>\n<p>You can exploit a quirk of the <code>ECHO<\/code> command: If you follow the word <code>ECHO<\/code> immediately with a period (no space), then the period is not echoed, but the presence of the period suppresses the &#8220;no command line&#8221; behavior, and you get a blank line. <\/p>\n<pre>\nECHO.\n<\/pre>\n<p>That prints a blank line. <\/p>\n<p>If you want to print a variable that might not be defined, you would say <\/p>\n<pre>\nECHO.%AWESUM%\n<\/pre>\n<p>This quirk of the <code>ECHO<\/code> command was originally a bug in the command line parser, but once people discovered it, the bug became elevated to a feature. (The phenomenon of a bug elevated to a feature <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20130710-00\/?p=3843\">happens<\/a> <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20110825-00\/?p=9803\">a lot<\/a>. <a HREF=\"https:\/\/xkcd.com\/1172\/\">Obligatory XKCD<\/a>.) <\/p>\n<p><b>Exercise<\/b>: Explain the output of this batch file, and how would you fix it so it prints <code>\/?<\/code>? <\/p>\n<pre>\nset message=\/?\necho %message%\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Because that&#8217;s what happens when you echo nothing.<\/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-96735","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Because that&#8217;s what happens when you echo nothing.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96735","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=96735"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96735\/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=96735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=96735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=96735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}