How to use "<<" in batch file or command prompt?

12

I know what >> is used for, it write all messages into a file instead of screen. And I guess << do reverse, I try it and receive a message: << was unexpected at this time.

Please tell me what is << used for and how to use.

DMaster

Posted 2015-06-21T01:56:54.573

Reputation: 223

Answers

17

The standard Windows command shell — cmd.exe — doesn't use the << operator at all

A single < means "read file into standard input" to cmd.exe, but two < characters back-to-back is meaningless to cmd.exe, so it gives the error you got.

The << operator is meaningful to all major types of Unix command shell, where it is used for here-documents

$ some-command <<END
blah blah blah
blah blah
blah blah blah blah blah
END

Those three lines get sent to some-command on its standard input.

This is useful for sending lots of text into a command without writing it to a file first, as you'd have to with the < operator. I frequently use it to embed a "usage" message into a script:

#!/bin/sh
if [ -z "$1" ]
then
    cat <<USAGE
usage: myscript <files...>

     Grobbles the foobie for all files given on the command line.

USAGE

    exit 1
fi

# ... do something with command line arguments

This is better than writing a bunch of echo statements since the heredoc text is formatted exactly like it prints to the screen. Additionally, it's easier to deal with white space, quoting, redirection, and variable interpolation in this context. Notice, for example, that I've used angle brackets in the usage message without having to do anything clever to prevent the shell from trying to use them for I/O redirection.

If you want to do things like this on Windows, you could install Cygwin and use one of its shells. If you are on Windows 10, you could use WSL instead.


Footnotes:

  1. That link goes into the archived Windows XP documentation tree. Microsoft broke the link I was previously using when they archived these docs, so in case they break it again, here is a backup third-party reference.

    The only other cmd.exe reference material I'm aware of on microsoft.com is the Windows Commands PDF (4.9 MB, 948 pages) which does little more than provide a reference for most (!) of the built-in and Microsoft-provided external commands you can give at the cmd prompt. This PDF is incomplete on two bases. First, and most relevant here, there is no combined discussion of how redirection works in the cmd.exe shell; there isn't even a discussion of shell grammar. Second, the PDF's command list is incomplete: the first thing I happened to check isn't covered: diskpart.

    I believe all of this follows from Microsoft's clear attempts to replace cmd.exe with PowerShell, which have been going on for many years now. In the most recent Windows 10 update as of this writing, they have taken further steps to hide the existence of cmd.exe, though it is not completely gone yet.

    It is worth noting that PowerShell also does not support a << redirection operator. Nor — in a sad regression from both Unix shells and cmd.exe — does it support < redirection!

  2. The canonical way to start a here-document is as I have written it above, with no space between the << and the delimiter word. My fuzzy recollection is that all uses of here-documents I've seen in shell scripts are also done this way. The POSIX specification for here-documents also uses this style in its examples.

    However, a careful reading of other parts of the POSIX.1-2008 specification reveals that it is legal to put some number of space or tab characters between the << and the delimiter word. In particular, see token recognition rules 7 and 10, the definition of io_here in the shell grammar, and the definition of the "blank" character class.

    That is how you document a shell. Take notes, Microsoft. ;)

    Testing here on Bash 4 and ksh93 confirms that this works as expected.

Warren Young

Posted 2015-06-21T01:56:54.573

Reputation: 2 587

Couldn't have said it better myself. Except I wouldn't have mentioned Cygwin because there's simply somethings you do in Windows and others you do with Unix. There's a lot of cross compatibility yes however I would strongly suggest just learning raw Unix rather than some virtual work around like Cygwin or the several other Win based utilities. – BiTinerary – 2015-06-21T02:18:21.917

8@BiTinerary: Given that a huge number of people use Cygwin daily specifically in order to be able to do Unix-like things on Windows, I think I'll leave that hint in the answer. – Warren Young – 2015-06-21T02:19:28.927

Yea, I don't mean to debase the authenticity that is Cygwin, hell I use it for mundane tasks now and then. I'm just putting in my two cents that is booting up the real thing and learning that (especially for beginners) is better than learning something 3rd party or a spin off of the original that has to conform to a completely different OS. I just mean to say that in the amount of time it takes to setup Cygwin, specify all the included modules + start using it, you could have learned a lot in the real thing. I feel like Cygwin is what you use after you've done that and just dont want to reboot – BiTinerary – 2015-06-21T03:42:08.097

Nit: (Unix) heredoc does not recognize redirection and pipe, but by default DOES recognize parameter/variable substitution and process substitution (both dollar-parens and backticks syntaxes); to suppress those use backslash in the data or quote (at least one char of) the delimiter word on the command line. – dave_thompson_085 – 2018-05-10T07:54:52.167

6

There is > and >> but only <, there is no <<

command < filename        Type a text file and pass the text to command

Source: http://ss64.com/nt/syntax-redirection.html

Shevek

Posted 2015-06-21T01:56:54.573

Reputation: 15 408

4

> writes to a NEW file.

>> appends to a file

< reads from a file

| sends a commands output into another command's input

See here for a list Is typing %^ into cmd.exe a Windows easter egg?

Since that was posted this has been added.

Starting a Program
===============

See start /? and call /? for help on all three ways.

Specify a program name
--------------------------------

    c:\windows\notepad.exe

In a batch file the batch will wait for the program to exit. When
typed the command prompt does not wait for graphical
programs to exit.

If the program is a batch file control is transferred and the rest of the calling batch file is not executed.

Use Start command
--------------------------

    start "" c:\windows\notepad.exe

Start starts a program and does not wait. Console programs start in a new window. Using the /b switch forces console programs into the same window, which negates the main purpose of Start.

Start uses the Windows graphical shell - same as typing in WinKey + R (Run dialog). Try 

    start shell:cache

Use Call command
-------------------------

Call is used to start batch files and wait for them to exit and continue the current batch file.

trigger

Posted 2015-06-21T01:56:54.573

Reputation: 79