It is even more convoluted than that. The above are just the rules for how programs
based on the Micorosft C library parse their command line into arguments. If you're
creating that command line in CMD.EXE, then you have to be aware of how CMD.EXE
munges things.
CMD.EXE processes only double quotes, not single quotes. When it sees a double quote,
it goes into quote mode, which means that special characters are not special, until the
next double quote.
These double quotes are *not* removed.
Special characters are %, ^, (, ), | and a few others. When these are outside of quotes,
they have to be escaped if you don't want their special meaning to apply. For instance,
the ampersand here means to run two commands in sequence:
C:\> echo foo & echo bar
foo
bar
You can escape that with quotes, but then the quotes get echoed:
C:\> echo foo "&" echo bar
foo "&" echo bar
(The echo command, clearly, is *not* an external program that is written using Microsoft's
C library and a main() function, because such a program would not see the quotes in
its argv[] array.)
You can escape with the caret. As you can see, it *is* consumed, unlike double quotes:
C:\> echo foo ^& echo bar
foo & echo bar
Double quotes can be escaped with carets like other special characters, to
suppress their meaning. Here the processing of % takes place between quotes:
C:\> echo ^"foo %PATH% bar^"
"foo [snip dump of PATH] bar"
Here it doesn't, but either way, the quotes are retained:
C:\> echo "foo %PATH% bar"
"foo %PATH% bar"
That's not all there is to it, but enough to get a basic understanding.
> I have git installed on my machine. When I try the following under git bash and awk or gawk, it gives the proper result:
> echo 123 | gawk '{print $) 7}'
Typo; should be $0
> 1237
> When I try the same with gawk under the cmd.exe, I get:
> echo 123 | gawk '{print $0 7}'
> 723
Here, the single quote means nothing to CMD.EXE. None of the characters are special, so the
gawk executable receives the command line as a single character string, quotes and all:
'{print $0 7}'
Something in the gawk program itself is dealing with the single quotes somehow,
according to some rules. Those rules are different from the Microsoft conventions
for parsing command lines, which do not recognize single quotes.
The output 723 looks like it could be result of 123 being printed, followed by
a carriage return (but not a newline). It's hard to understand why that would
happen; why would 123 be printed with no newline.
> This is version GNU Awk 3.0.4
I suspect that the gawk that you're accessing from cmd.exe and the one from
bash are not the same program.
> Guess I will stick with bash/(gawk or awk). Or stay away from cmd.exe for such.
CMD.EXE offers a poor language if you're using external tools which involve
writing in an entire programming language which is embedded in a command line
argument. It's mainly suitable for passing simple tokens to dumb,
non-programmable tools.
If you launch awk from cmd.exe, you're probably best off in putting the script in
a file and using "awk -f file".