2

Let me take bundle.bat file from RubyInstaller to represent an example.

@ECHO OFF
IF NOT "%~f0" == "~f0" GOTO :WinNT
@"ruby.exe" "C:/Ruby200/bin/bundle" %1 %2 %3 %4 %5 %6 %7 %8 %9
GOTO :EOF
:WinNT
@"ruby.exe" "%~dpn0" %*

I don't understand this:

  1. What does @ mean here @"ruby.exe" and what for double quotes?

Here in the manual I found some explanation:

Explanation - the first line prevents the commands from being displayed, the @ in "@echo off" keeps that line from displaying.

And here are my two test scripts.

The first one:

@ECHO OFF
@ECHO "123"
ECHO "123"
PAUSE

The output:

C:\win>batch.bat
"123"
"123"

@ does't keep line from displaying.

The second:

I want to call gem environment command from batch file. And here I'm guided by the code from bundle.bat file. My code:

@ECHO OFF
@"gem environment"
PAUSE

The output is an error. Please notice two double quotes:

""gem environment"" is not a command or executable or package file.

But in bundle.bat the line @"ruby.exe" "%~dpn0" %* works.

Now I change my script:

@ECHO OFF
REM Call like this...
@gem environment
REM or call like this.
gem environment
PAUSE

Both work fine. The output:

RubyGems Environment:
  - RUBYGEMS VERSION: 2.0.3
  - RUBY VERSION: 2.0.0 (2013-02-24 patchlevel 0) [i386-mingw32]
  - INSTALLATION DIRECTORY: C:/Ruby200/lib/ruby/gems/2.0.0
  - RUBY EXECUTABLE: C:/Ruby200/bin/ruby.exe
  - EXECUTABLE DIRECTORY: C:/Ruby200/bin
  ... and so on

So my questions are:

  1. What is the meaning of @ character in Batch script?
  2. When do I have to double-quote the value after @?
Green
  • 28,742
  • 61
  • 158
  • 247

1 Answers1

7

Question 1

Q1. What is the meaning of @ character in Batch script?

The @ character does indeed prevent the line from being displayed. It does not prevent the output of the line from being displayed, if there is any.

So for example:

echo foo

would display this:

echo foo
foo

But add the @ like this:

@echo foo

and all you get is the output:

foo

Once you've turned echo off, the @ is useless because none of the lines are being displayed anyway.


Question 2

Q2. When do I have to double-quote the value after @?

The double-quotes in your sample code have nothing to do with the @. All the @ does is cause the line to not be displayed.

Double-quotes are useful to point to a file that contains spaces in its path. For example, if you try to run a programme like this:

C:\Program Files\foo.exe

the shell will only parse that up to the first space, so it looks for a file or command named C:\Program, and would return an error.

Change it to this:

"C:\Program Files\foo.exe"

and it will correctly look for C:\Program Files\foo.exe and run it if it exists.

In your case if you just say gem environment, it can find the programme named gem.exe and pass in environment as a parameter. But if you quote it, "gem environment", it is looking for something named gem environment.exe.

Nate Hekman
  • 6,507
  • 27
  • 30