1

I'm trying to execute a command with git-bash using cmd. I need to pass the command for it to execute. The problem is I need to pass the path of the batch file into the command and it has spaces. I can't seem to be able to pass quotes to "cmd" in a way that it will understand it.

cmd //C "c:\Program Files (x86)\another path\file.bat args && echo done"

That will give me an error:

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

If I try using single quotes then it passes a literal backslash to cmd.

cmd //C '"c:\Program Files (x86)\another path\file.bat" args && echo done'

Which will give me the error the same error but show the backslashes:

'\"c:\Program Files (x86)\another path\file.bat\"' is not recognized as an internal or external command, operable program or batch file.

razr32
  • 339
  • 4
  • 16

2 Answers2

1

For CMD /C, see "Bash in Git for Windows: Weirdness when running a command with CMD.exe /C with args"

Try using a Unix-style path, within double-quotes

$ CMD //C "/c/Program Files (x86)/Opera/New folder/a.bat"

(no && echo done)

Tested with git version 2.11.0.windows.1 on Windows 10.

Note: For simple commands, you don't need a CMD /C to execute a bat from a git bash session. In that case, you can escape spaces and parenthesis:

vonc@vonc MINGW64 /c/Users/vonc
$ /c/Program\ Files\ \(x86\)/Test/New\ folder/a.bat

The OP adds in the comments:

I want the environment variables to only exist on the one instance of cmd. I don't want it to actually set the environment variables in bash.
Which is why the "&&" is necessary, as the other command would rely on those variables. That's the only command I need to rely on them for.
cmd /C "C:/path to/vcvarsall.bat && echo %LIB%" should print the env variable set by the batch file.
Then in git-bash I want echo $LIB to print nothing after executing the command.

echo %LIB% is tricky because the CMD session would use the echo from Git, instead of the echo builtin CMD shell command.
But chaining multiple command in a CMD (adapted here for a Git bash session) is done with:

cmd //V //C "C:/path to/vcvarsall.bat&&set myvar"

set myvar will display the value set for myvar by the bat script.
And yet, back in the bash session, an echo ${myvar} would not display anything.

bleater
  • 5,098
  • 50
  • 48
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The batch script doesn't work in git-bash though. If it helps it's vcvarall.bat for visual studio. – razr32 Jan 29 '17 at 06:02
  • Not really an error so much as the batch script doesn't do anything, which it is suppose to add variables to the environment. I wanted to use cmd as it works in there, but I need the "&&" bit as the program has to run in that environment with those symbols loaded. This would also be preferred as I don't want the environment variables loaded into the current bash as they cause conflict. – razr32 Jan 29 '17 at 07:22
  • @user240713 Add variable to the environment? That would not work in a CMD /C session: it opens a CMD, then quit. Any environment variable set is lost. Could you wrap all you need to do in a .bat which would call vcvarall.bat and does the rest, as opposed to mix and match Windows and bash commands? – VonC Jan 29 '17 at 07:37
  • Yes I want the environment variables to only exist on the one instance of cmd. I don't want it to actually set the environment variables in bash. Which is why the "&&" is necessary. As the other command would rely on those variables. That's the only command I need to rely on them for. `cmd /C "C:/path to/vcvarsall.bat && echo %LIB%"`. Should print the env variable set by the batch file. Then in git-bash I want `echo $LIB` to print nothing after executing the command. – razr32 Jan 29 '17 at 16:25
  • @user240713 easy (http://stackoverflow.com/a/31824572/6309): `cmd /V /C "C:/path to/vcvarsall.bat&& echo !LIB!"`: note the `/V` (enable delayed expansion), the lack of space between `.bat` and `&&`, and the `!LIB!` instead of `%LIB%`. – VonC Jan 29 '17 at 16:29
  • Running that from git-bash I get `bash: !LIB!: event not found`. – razr32 Jan 29 '17 at 16:39
  • @user240713 I just tested it in a Git bash session, and indeed the `echo` does not work (because it takes the `echo` from Git instead of the builtin CMD `echo`). But if you replace `echo !LIB!` by `set LIB`, you will see that `LIB` is properly valued. – VonC Jan 29 '17 at 17:04
  • @user240713 I have edited the answer to include the comments here, for the solution to be more visible. – VonC Jan 29 '17 at 19:23
0

And another simple way is to navigate to the directory(using cd command) that contains that file and then run the command and pass the filename.

Anil Sharma
  • 128
  • 4
  • 19
  • You don't need that: you can directly reference the script, without using CMD or changing path. See my answer. – VonC Jan 29 '17 at 05:51