7

gcloud.cmd is a Windows command-line script. I am trying to run it from the Bash shell installed on Windows 10. It is recognized by the CMD prompt, but not by “Bash for Windows 10”.

Based on this thread I created a .bashrc file with this entry:

PATH=$PATH:/mnt/c/Users/username/AppData/Local/Google/Cloud SDK/google-cloud-sdk/bin

It seems that Bash now finds the file because when I run gcloud.cmd it shows:

/mnt/c/Users/***/AppData/Local/Google/Cloud SDK/google-cloud-sdk/bin/gcloud.cmd: line 1: @echo: command not found
/mnt/c/Users/***/AppData/Local/Google/Cloud SDK/google-cloud-sdk/bin/gcloud.cmd: line 2: rem: command not found
/mnt/c/Users/***/AppData/Local/Google/Cloud SDK/google-cloud-sdk/bin/gcloud.cmd: line 7: syntax error near unexpected token `newline'
/mnt/c/Users/***/AppData/Local/Google/Cloud SDK/google-cloud-sdk/bin/gcloud.cmd: line 7: `rem <cloud-sdk-cmd-preamble>'

I also tried the following commands because Cygwin seems to recognize .bat files automatically:

cmd.exe gcloud.cmd
cmd gcloud.cmd 
cmd gcloud
cmd /c gcloud
cmd /c glcoud.cmd

All of the above commands show:

No command 'cmd' found, did you mean: (…)

How do I run Windows Batch commands from Bash?

Maëlan
  • 3,586
  • 1
  • 15
  • 35
alpha_989
  • 4,882
  • 2
  • 37
  • 48
  • 2
    I haven't used bash on windows so don't know this will work, but have you tried `cmd /c gcloud` or similar, so the `gcloud` command would be executed in a `cmd` shell instead of directly in the `bash` shell? – Eric Renouf Jun 24 '17 at 17:52
  • Yeah.. I was just thinking about this. It seems CYGWIN runs .bat files automatically in the native interpreter (https://stackoverflow.com/questions/787522/why-is-it-that-cygwin-can-run-bat-scripts). I have tried the following cmd.exe gcloud.cmd; cmd gcloud.cmd; cmd gcloud; cmd /c gcloud; cmd /c glcoud.cmd – alpha_989 Jun 24 '17 at 18:05
  • none of the above work.. perhaps I should move to cygwin, or install linux in a virtual machine.. – alpha_989 Jun 24 '17 at 18:06
  • 1
    It's hard for people to find information in the comments, and they don't format well, Instead, please [edit](https://stackoverflow.com/posts/44739204/edit) your question to include the updated attempts, and it would probably be helpful to include exact error messages when available instead of a variation on "it didn't work." – Eric Renouf Jun 24 '17 at 18:07
  • ok.. no problem! – alpha_989 Jun 24 '17 at 18:09
  • 2
    Have you tried to use a full path to cmd.exe? as in `/mnt/c/windows/system32/cmd.exe /C gcloud.cmd` You may also want to include /mnt/c/windows/system32/ to your PATH – Michaël Roy Jun 24 '17 at 18:38
  • Thanks.. i just did. Didnt work unfortunately. The only way I found it to work is by getting out of the bash shell using the following in sequence 1. cmd.exe 2. gcloud.cmd 3. exit. however, I was trying to run gcloud.cmd along with some parameters, and pass the output to grep. Grep is installed in the bash shell, and not in the windows command. So I couldnt get it working.. – alpha_989 Jun 24 '17 at 18:43
  • also the PATH maynot be issue here.. because cmd.exe is recognized regardless of which directory I am in, in the bash shell. – alpha_989 Jun 24 '17 at 18:48
  • 3
    Did you read [Bash on Ubuntu on Windows - Bash-Windows Interop](https://msdn.microsoft.com/en-us/commandline/wsl/interop)? –  Jun 24 '17 at 21:11
  • Thanks LotPings. I think I maybe able to work with this.. – alpha_989 Jun 24 '17 at 22:32
  • The `PATH=…` line looks wrong because it contains a space: it should be double-quoted or escaped. – Maëlan Jun 24 '22 at 13:28

2 Answers2

7

You need to specify the full path to cmd.exe.

I have added the following to my ~/.bash_aliases:

alias cmd='/mnt/c/Windows/System32/cmd.exe /c'

with this you can run *.bat files with:

$ cmd ./test.bat
  • The alias method works, however you will have to adjust the path, because `cmd` will not be able to comprehend `./test.bat`. So you will either have to simply pass `test.bat` without `./` if you are in the directory where your batch file is located, or you need to pass the full path, *i.e.* use a command like this: `cmd 'C:\MyFolder\MySubfolder\test.bat'`. – Koenigsberg Sep 07 '18 at 07:08
2

Sorry to post this as an answer but I cannot yet comment. I was thinking that maybe you could have a batch file that calls bash scripts, then once they exit, it starts to bat files, then starts more bash scripts? You could split your code in too... Just an idea.

Luke Deven
  • 66
  • 1
  • 8