Reputation: 7709
I have the following command:
git log --pretty=tformat:'<li>%h %ci %d %s</li>' > changelog.html
That saves the git log to the changelog.html file. When i execute it through git bash, it works fine, but when i put this code in a .bat file and run it, i get the following error:
The system could not find the specified file
I think it's caused by the quotes on the format parameter, but i don't know how to solve the problem... Is there a way to escape the quotes maybe?
Upvotes: 4
Views: 1883
Reputation: 440501
Inside a Windows batch file, you have to:
%
chars. for them to be taken as literals>
:git log --pretty=tformat:"<li>%%h %%ci %%d %%s</li>" > changelog.html
^
to escape individual characters in an unquoted string (e.g., ^%
to represent literal %
, or ^|
to represent literal |
), but that wouldn't work here, because of the following caveat:^
to escape a space character in order to form a single argument with embedded whitespace; for instance, foo^ bar
is still split into 2 arguments, foo
and bar
.Some background for readers from the Unix world:
for
loop) - they canNOT be used to delimit a string with embedded whitespace.$
in double-quoted bash strings as \$
to prevent expansion, you have to escape %
as %%
in double-quoted strings to prevent expansions in batch files, given that an identifier enclosed in %
chars. (e.g., %PATH%
) signifies a variable reference to expand.
cmd.exe
prompt doesn't require escaping of %
, whereas batch files do. More specifically, %
chars. in an interactive command line are treated as literals, unless they are part of a variable reference to an existing variable (e.g., %windir%
), in which case expansion does take place. This oddity is the reason that a for
loop must be written as for %i in ...
(one %
) on the command line, and as for %%i in ...
(two %
) in a batch file.~
parameter modifier can be used to strip them; e.g., %~1
returns the 1st argument without double quotes (if any).\
-escaping individual characters in an unquoted string in a POSIX shell is ^
-escaping, but, as stated, ^
cannot be used to escape a space in order to form a single argument with embedded whitespace.Upvotes: 13