8

I'm have started to learn batch programming to go a little more deeper in the Windows Machines. In Internet I have seen some commands with echo like "@echo off" or "on" and also this: "echo." but I don't know what are they doing. If anyone can explain me the functions of the echo command, please answer me.

Antonio Gschossmann
  • 472
  • 1
  • 7
  • 17
BAR Software
  • 113
  • 1
  • 1
  • 6
  • 1
    echo controls the output. if you con't want to see any commands that the batch file runs you call "echo off". if you don't want to see the command echo off itself, you add '@' before it. – Tamar Jun 16 '17 at 11:36
  • 1
    You can find out the purpose and usage of any batch command by typing the command and following it with `/?` - in this case, you'd type `echo /?` – SomethingDark Jun 16 '17 at 13:36

3 Answers3

9

The ECHO command in Windows CMD is used to print out text to the screen, display the actual setting of the Command-line which means when you do:

@echo off

The "C:\Users[User]" line before your command input will disappear. You can restore it with:

@echo on

Here all functions of ECHO explained:

  • @echo [on/off] (to set the command-line settings)

  • echo [text] (to print text to the screen)

  • echo. (to print an empty line to the screen)

  • echo (displays the current setting of the command-line)

  • echo /? (displays help for the command ECHO)

I hope I was able to help you.

Antonio Gschossmann
  • 472
  • 1
  • 7
  • 17
  • 1
    I wish to add that `@echo on` is also useful to *debug* batch scripts, to see what's going on when something seems not to work. – elzooilogico Jun 16 '17 at 12:34
  • 4
    It's better to use `echo(`, as said by dbenham it always works (https://stackoverflow.com/a/3123194). – man Jun 16 '17 at 12:34
  • @elzooilogico Yes, for only "command" applications. But if you want make an UI, it's better if you use echo off , because it's more comfortable and cleaner. – Antonio Gschossmann Jun 16 '17 at 12:38
  • 1
    yes, always write `@echo off` in top of any script, but at development time, is helpful to turn it `on` above a problematic section of code to see *command expansion* and *its output*. of course, once the bug or typo is corrected, it must be removed. – elzooilogico Jun 16 '17 at 13:02
4

By default, every command executed in a batch file is also echoed to the output - not just the output of the command, but the command itself.

The echo command has three modes:

  1. When called with some arguments AFTER a space, it will output the arguments.
  2. When called as echo. (no space) it will output just a blank line.
  3. When called with just the arguments on or off, it controls the behaviour of printing the commands as they execute.*

So, the echo off command turns off the output at the start of the batch file. However, that command itself is still echoed before it has a chance to turn off the echoing. The @ symbol has the effect of turning off the output for only the current command.

Combining the two, the @echo off at the start of a batch file turns off the echoing without itself being echoed.

(* I'm not sure exactly how to output the text "on" or "off", but I suspect that using quotes around the text will change the behaviour.)

Harun
  • 1,013
  • 8
  • 7
  • 3
    In the presumably unusual case of wanting to echo _just_ the word **off** or **on** (i.e., not a part of any other string), it turns out that `echo.off` and `echo.on` do the trick. – Jeff Zeitlin Jun 16 '17 at 11:52
  • 3
    @JeffZeitlin, it works for the same reason as `echo.` The "." isn't a whitespace character, so it won't parse `echo.on` as the `echo on` command. Next this trick takes advantage of how the `echo` command assumes that the first character in the argument string is white space to be skipped over, except in this case it's skipping the ".". – Eryk Sun Jun 16 '17 at 12:04
  • Very interesting! I didn't realise that the echo. form could have more text after it (nor that it didn't have to be a period, as Kappa points out in a comment on a different answer). Quotes are just echoed to the output, so that guess was no good. – Harun Jun 21 '17 at 09:22
3

Partial answer is this: What does "@" mean in Windows batch scripts

The @ before the command means do not print that command when running it.

The off argument tells the script not output any other commands, however without the @ would output the echo off (since echoing hasn't yet been turned off)

The on argument turns command echoing back on.

Any other arguments are just echoed to the display

lostbard
  • 4,460
  • 1
  • 10
  • 15