Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "@echo off" and "@echo %off"?

Tags:

batch-file

At the beginning of a batch script, I saw the command:

@echo %off

To my surprise it has the same effect of:

@echo off

What is the effect of the '%' prefix?

like image 584
Claudiu Avatar asked Jul 11 '16 12:07

Claudiu


1 Answers

I've never seen it before. It doesn't work in the cmd console -- only in a .bat script. But I have a guess.

In a cmd console window if you @echo off, that results in command prompts being hidden, just like it does in a .bat script. To reveal the prompts again, you have to echo on. The difference is that in a .bat script, percent signs need to be doubled to represent a literal % string character, whereas in the cmd console they do not. The result is that @echo %off in a cmd console results in the string %off being echoed to stdout.

With this in mind, I'm guessing the author intended this hack to avoid problems encountered by users who copypaste the script into a cmd console window, rather than as intended into a .bat script. With % added, the command is neutered in the console, but still achieves its intended effect when run from a .bat script. Without the %, the console would appear to hang after all instructions have completed.

hung console

like image 125
rojo Avatar answered Nov 09 '22 01:11

rojo