Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing windows slash (/) based parameters to a program from bash script

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...

like image 439
Tracker1 Avatar asked Jan 07 '16 04:01

Tracker1


People also ask

What does %% mean in bash?

So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage: name A word consisting only of alphanumeric characters and under- scores, and beginning with an alphabetic character or an under- score.

What is forward slash in bash?

In Linux and other Unix-like operating systems, a forward slash is used to represent the root directory, which is the directory that is at the top of the directory hierarchy and that contains all other directories and files on the system.

How do you call a parameter from a function in shell script?

To invoke a function, simply use the function name as a command. To pass parameters to the function, add space-separated arguments like other commands. The passed parameters can be accessed inside the function using the standard positional variables i.e. $0, $1, $2, $3, etc.


2 Answers

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

like image 152
M. Mimpen Avatar answered Sep 23 '22 20:09

M. Mimpen


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" 
like image 28
Tracker1 Avatar answered Sep 22 '22 20:09

Tracker1