40

I'm trying to run the following from my bash script. (bash installed via msysgit)

taskkill /IM ssh-agent.exe

The output I get on the screen from taskkill is:

ERROR: Invalid argument/option - 'C:/Program Files/Git/IM'.
Type "TASKKILL /?" for usage.

The executible is running, but the /IM is being expanded regardless of what I try to do to escape it...


I've tried using \/IM but then it sends \/IM without escaping the slash, I've tried a few different ways of running it through eval, cmd /c start, etc... but they all seem to have issues. I've also tried set -o noglob, which also didn't work. neither did $'\057/'IM or similar attempts...

3
  • Maybe try .\SomeExecutable.exe "/foo"
    – l'L'l
    Jan 7, 2016 at 4:54
  • @l'L'l SomeExecutable is running fine, it's the fact that the parameter coming into SomeExecutable is /some/path/to/foo instead of just /foo
    – Tracker1
    Jan 7, 2016 at 4:58
  • Oh I see... escaping it seems logical as you had; my only other suggestion might be to try it in quotes.
    – l'L'l
    Jan 7, 2016 at 5:04

2 Answers 2

44

Since my comment actually provided the answer, I'll post it.

How about escaping a forward slash to another forward slash like //. It works for me when I execute this command where I escaped the /r parameter: start C:/folder/beep 2000 250 100 //r 3

Source: http://oldwiki.mingw.org/index.php/switches%20with%20forward%20slashes

Minimalist GNU for Windows

Passing switches with forward slashes under MSYS

In MSYS a command line argument of "/c" is interpreted as the C: drive, so to pass any argument beginning with a forward slash you need to use two forward slashes. For example, to use this command in MSYS:

cmd /c echo foo

Use:

cmd //c echo foo

If you need to have the windows-style of a path in a shell script, you can do

x=$(cd /unix/path && cmd //c cd)

The x var now contains the windows equivalent path to /unix/path

3
  • The trick for converting unix path to windows path in a variable is very useful. However, you should be aware that if /unix/path is a symblink, e.g. created by mklink /j c:\unix\path d:\unix\path, your variable will show the linked location d:\unix\path
    – sveinbr
    Aug 16, 2017 at 13:27
  • 2
    @sveinbr: Thank god msys2 comes with cygpath again, eh? I remember being thoroughly frustrated at some version of msys whose maintainers had left cygpath out, apparently just because "You should not need such a thingy, because Msys automagically translates Path names, even in the environment.".
    – SamB
    Jul 1, 2019 at 20:39
  • However, if we are running a huge bash script in MinGW, we cannot modify the script to make it working with mingw syntax...
    – recolic
    Apr 28, 2022 at 9:45
5

After hours of looking for various searches like "disable bash file expansion" and the like, I found it by searching specifically for "bash" "windows" taskkill the executable I was trying to run, I came across this answer, which finally worked for me.

cmd " /c taskkill /F /IM ssh-agent.exe"
2
  • 3
    Can't you simply escape the forward slash with another forward slash like //? It seems to work for me, source: oldwiki.mingw.org/index.php/switches%20with%20forward%20slashes
    – M. Mimpen
    Aug 25, 2016 at 8:52
  • 1
    @M.Mimpen thanks so much, that actually works for me too... I'd tried \/ and can't think of how many other permutations to try and get bash to not use path completion. I was trying to make my windows start-ssh-agent match the cmd based one...
    – Tracker1
    Aug 25, 2016 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.