public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Re: syntax for Cygwin bash invoking Win apps
@ 2009-09-09 15:27 Ziser, Jesse
  2009-09-09 15:57 ` Mark J. Reed
  2009-09-09 16:10 ` Larry Hall (Cygwin)
  0 siblings, 2 replies; 14+ messages in thread
From: Ziser, Jesse @ 2009-09-09 15:27 UTC (permalink / raw)
  To: cygwin

>On 09/08/2009 11:30 PM, Ziser, Jesse wrote:
>> Hello list,
>>
>> When I type a command in bash to invoke a Windows application (like
>> cmd.exe, for example), I can't seem to find a pattern in the Windows command
>> line that actually gets executed. Ordinary bash syntax does not seem to
>> apply in general when the command is a Windows app, but rather, sometimes
>> special characters are interpreted in a bash-like way, and sometimes not.
>> So, I'm wondering what determines whether a quote mark or something gets
>> interpreted or passed on.
>>
>> Here are some examples:
>>
>> $ cmd /c echo "/?"
>> Displays messages, or turns command-echoing on or off.
>>
>>    ECHO [ON | OFF]
>>    ECHO [message]
>>
>> Type ECHO without parameters to display the current echo setting.
>>
>> # OK, so I'm getting the Windows echo, not the bash echo.  Good.
>> # Moving on...
>> $ cmd /c echo abc
>> abc
>>
>> $ cmd /c echo "abc"
>> abc
>>
>> $ cmd /c echo "\"abc\""
>> "\"abc\""
>>
>> # Wahhh?!
>>
>> Anyone who knows the explanation would make me very grateful. I've tried
>> this with other Windows apps too, and the same weirdness seems to occur.
>
>All of the above is consistent with bash shell quoting.  It's the shell that 
>does the interpreting in the Unix/Linux world and that's what you're seeing here.

Huh?  Last time I checked, bash translates "\"abc\"" to "abc".

>> On a related note, I've noticed what appears to be an automatic sort of
>half-bash invocation (but not quite?) or something when I run Cygwin
>commands from cmd.exe. For example,
>
>>> c:\cygwin\bin\echo hi
>> hi
>>
>>> c:\cygwin\bin\echo "hi"
>> hi
>>
>>> c:\cygwin\bin\echo "\"hi\""
>> "hi"
>>
>>> c:\cygwin\bin\echo *
>> myfile myotherfile yetanotherfile ...
>>
>> And yet...
>>
>>> c:\cygwin\bin\echo $PATH
>> $PATH
>>
>> What the heck is going on? Are there any rules here at all? Sorry if I'm
>> missing something dumb. And sorry for apologizing for it. And......
>
>In this case, the Cygwin DLL intercedes and handles quoting for the Cygwin 
>app that
>you invoked (echo).  But it only does quoting.  You're mixing the notion of 
>quoting with
>environment handling.  They are two different things.

Does "handles quoting" mean that it implements the "Quoting" section, and only that section, of the bash manpage?  I just need to know exactly what it does.  It clearly does not *only* do quoting.  That's why I demonstrated the asterisk example.  It is doing at least wildcard expansion in addition to quoting.  What else is it doing?  I'm trying to figure this out so I know how to properly escape or quote a general command in order to execute it from a Windows application without any unexpected changes.

Thanks for the response,
Jesse



      

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 14+ messages in thread
* Re: syntax for Cygwin bash invoking Win apps
@ 2009-09-09 15:57 Ziser, Jesse
  2009-09-22 19:28 ` Ziser, Jesse
  0 siblings, 1 reply; 14+ messages in thread
From: Ziser, Jesse @ 2009-09-09 15:57 UTC (permalink / raw)
  To: cygwin

>On Tue, Sep 8, 2009 at 11:30 PM, Ziser, Jesse wrote:
>> Hello list,
>>
>> When I type a command in bash to invoke a Windows application (like cmd.exe,
>> for example), I can't seem to find a pattern in the Windows command line that
>> actually gets executed.  Ordinary bash syntax does not seem to apply in
>> general when the command is a Windows app, but rather, sometimes special
>> characters are interpreted in a bash-like way, and sometimes not.  So, I'm
>> wondering what determines whether a quote mark or something gets interpreted
>> or passed on.
>
>I can't contend to be an expert on cmd.exe escaping, but I'll take the first
>swing...  And the MS docs found at [1] will probably be more useful
>still.

Heh, that's OK, I don't need any help with cmd.exe escaping.  cmd.exe generally doesn't do any quote or escape replacement (except in a few rare cases).  In Windows, the command itself is usually responsible for that sort of thing.

>> Here are some examples:
>>
>> $ cmd /c echo "/?"
>> Displays messages, or turns command-echoing on or off.
>>
>>   ECHO [ON | OFF]
>>   ECHO [message]
>>
>> Type ECHO without parameters to display the current echo setting.
>
>Falls under the expansion conditions mentioned at the "processing
>quotation marks" section of the cmd manual; so it expands to run the
>command 'echo' with the argument '/?'

Nope.  The Windows "echo" does not remove quotes.  In this case, bash is removing the quotes.  This is a bash command line.  Also, cmd /c does not remove quotes, except when they are around the whole entire argument to cmd /c, which is not the case here.  To see this, try this at the Windows command prompt:

> cmd /c echo "hi"
"hi"

>> # OK, so I'm getting the Windows echo, not the bash echo.  Good.
>> # Moving on...
>> $ cmd /c echo abc
>> abc
>
>No quoting...
>
>> $ cmd /c echo "abc"
>> abc
>
>One set of quotes that is stripped, per the DOS rules ("If the previous
>conditions are not met, string is processed by examining the first
>character to verify whether or not it is an opening quotation mark. If
>the first character is an opening quotation mark, it is stripped along
>with the closing quotation mark.  Any text following the closing
>quotation marks is preserved.").  Granted, that rule doesn't make a lot
>of sense, but that's why we all prefer UNIX...  >_>

Nope.  The rule you just quoted specifically says it looks at the first character of the string to determine whether it is a quotation mark.  The first character of the string here is the letter "e".  Besides, this is a bash command line, so bash should be the one removing the quotes.

>> $ cmd /c echo "\"abc\""
>> "\"abc\""
>>
>> # Wahhh?!
>
>Again, a weird MS quoting behavior.  The "previous conditions" mentioned
>above include "you use exactly one set of quotation marks".  Here, you
>didn't, so cmd is nice enough to not strip anything for you.

But the above is a bash command.  Bash should remove the quotes and replace the escape sequences with literal quotes, shouldn't it?  But I do agree that if, for some reason (a reason I want to know), bash knew not to do quote replacement on this command, cmd.exe would produce exactly the output shown for this case.

>> Anyone who knows the explanation would make me very grateful.  I've tried
>> this with other Windows apps too, and the same weirdness seems to occur.
>>
>> On a related note, I've noticed what appears to be an automatic sort of
>> half-bash invocation (but not quite?) or something when I run Cygwin commands
>> from cmd.exe.  For example,
>>
>>> c:\cygwin\bin\echo hi
>> hi
>
>Nothing bash about this... Calling c:/cygwin/bin/echo.exe with the
>single argument 'hi'
>
>>> c:\cygwin\bin\echo "hi"
>> hi
>
>Nor this; it expands to exactly the same call

Nope.  To my knowledge, neither command.com nor cmd.exe ever does quote removal on any command typed at the command prompt.  That should be passing literal quotation marks to echo.exe.

>>> c:\cygwin\bin\echo "\"hi\""
>> "hi"
>
>This one I'm not sure about, but I think the reason is that the above
>crazy quoting rules only apply to "cmd /c" and "cmd /k", and that this
>undergoes a different sort of escaping where it is transformed to a call
>to echo.exe with just the argument '"hi"'.  Should be easy enough to
>test by using DOS echo instead of cygwin echo, but I don't have access
>to Cygwin ATM.

As I said above, using the Windows echo at the Windows command prompt does not do any quote removal whatsoever, and it most CERTAINLY does not treat backslash as an escape character!  Backslashes are an essential piece of the filesystem syntax in Windows!

>>> c:\cygwin\bin\echo *
>> myfile myotherfile yetanotherfile ...
>
>Nothing bash-related about this, either.  cmd.exe expanded the * to
>a list of files, just like you'd expect.

Why on Earth would you expect that?!  What cmd.exe are you using?  Are you using PowerShell or something?

>"echo *" should work fine in
>DOS, even without any cygwin tools, so this just expanded to a call to
>"echo.exe" with 3 arguments, "myfile", "otherfile", and
>"yetanotherfile".

Maybe your DOS, but not mine!  Did you really try it and get that result under DOS?  I've been using DOS since v3.2 and have never seen behavior like that.

>> And yet...
>>
>>> c:\cygwin\bin\echo $PATH
>> $PATH
>
>This one should make the most sense of all.  cmd.exe doesn't expand
>$ENVVAR (unix syntax for an environment variable), it expands %ENVVAR%
>(dos syntax), so naturally $PATH is passed straight through.  In
>a normal unix environment, $PATH would be expanded by a shell like bash
>or csh, which you've left out of that interaction; /bin/echo certainly
>isn't expected to check its arguments for possible environment variables
>and expand them itself.

That, I would expect, but given the other things that appear to happen, this behavior seems inconsistent.



      

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 14+ messages in thread
* syntax for Cygwin bash invoking Win apps
@ 2009-09-09  3:30 Ziser, Jesse
  2009-09-09  4:31 ` Matt Wozniski
  2009-09-09  4:40 ` Larry Hall (Cygwin)
  0 siblings, 2 replies; 14+ messages in thread
From: Ziser, Jesse @ 2009-09-09  3:30 UTC (permalink / raw)
  To: cygwin

Hello list,

When I type a command in bash to invoke a Windows application (like cmd.exe, for example), I can't seem to find a pattern in the Windows command line that actually gets executed.  Ordinary bash syntax does not seem to apply in general when the command is a Windows app, but rather, sometimes special characters are interpreted in a bash-like way, and sometimes not.  So, I'm wondering what determines whether a quote mark or something gets interpreted or passed on.

Here are some examples:

$ cmd /c echo "/?"
Displays messages, or turns command-echoing on or off.

  ECHO [ON | OFF]
  ECHO [message]

Type ECHO without parameters to display the current echo setting.

# OK, so I'm getting the Windows echo, not the bash echo.  Good.
# Moving on...
$ cmd /c echo abc
abc

$ cmd /c echo "abc"
abc

$ cmd /c echo "\"abc\""
"\"abc\""

# Wahhh?!

Anyone who knows the explanation would make me very grateful.  I've tried this with other Windows apps too, and the same weirdness seems to occur.

On a related note, I've noticed what appears to be an automatic sort of half-bash invocation (but not quite?) or something when I run Cygwin commands from cmd.exe.  For example,

> c:\cygwin\bin\echo hi
hi

> c:\cygwin\bin\echo "hi"
hi

> c:\cygwin\bin\echo "\"hi\""
"hi"

> c:\cygwin\bin\echo *
myfile myotherfile yetanotherfile ...

And yet...

> c:\cygwin\bin\echo $PATH
$PATH

What the heck is going on?  Are there any rules here at all?  Sorry if I'm missing something dumb.  And sorry for apologizing for it.  And......

Thanks in advance,
Jesse






--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2009-09-22 19:28 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-09-09 15:27 syntax for Cygwin bash invoking Win apps Ziser, Jesse
2009-09-09 15:57 ` Mark J. Reed
2009-09-09 16:05   ` Christopher Faylor
2009-09-09 16:10     ` Mark J. Reed
2009-09-09 16:31       ` Ziser, Jesse
2009-09-09 17:45       ` Christopher Faylor
2009-09-09 18:21         ` Ziser, Jesse
2009-09-09 18:41           ` Mark J. Reed
2009-09-09 16:10 ` Larry Hall (Cygwin)
  -- strict thread matches above, loose matches on Subject: below --
2009-09-09 15:57 Ziser, Jesse
2009-09-22 19:28 ` Ziser, Jesse
2009-09-09  3:30 Ziser, Jesse
2009-09-09  4:31 ` Matt Wozniski
2009-09-09  4:40 ` Larry Hall (Cygwin)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).