How to use "<<" in batch file or command prompt?
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:
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 thecmd
prompt. This PDF is incomplete on two bases. First, and most relevant here, there is no combined discussion of how redirection works in thecmd.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 ofcmd.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 andcmd.exe
— does it support<
redirection!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 ofio_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.
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
>
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.