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

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

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

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"

Related

Clear PowerShell console in bash on Windows

I download bash.exe from SourceForge and added it to my path in Powershell, but I can't get it to clear the console. clear.exe is missing from the zipfile that was downloaded, so it makes sense that that command doesn't work. However, using Ctrl+L also does not clear the powershell console.
How can I get the powershell console to clear when I'm using bash in it?
Note: I've tried adding an alias called clear to my .bashrc as alias clear=echo <many enters>, but it doesn't work quite the way I've expected (i.e. only echoes 4 or 5 newlines). Also, echo "\n\n" just prints out literal \n\n.
In the absence of a clear or tput utility, and given that the usual ANSI escape sequences don't work with the (built-in) printf, you must call out to either cmd.exe or PowerShell to effect clearing the screen:
bash$ powershell -noprofile -c cls
Using cmd is faster, but the problem is that the win-bash invokes external programs by double-quoting each argument behind the scenes, which causes a command such as cmd /c cls to malfunction; the following workaround mostly works, but prints the cmd.exe prompt string once after clearing the screen.
# !! Clears the screen, but prints the cmd.exe prompt string once.
bash$ echo cls | cmd

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

This is more of an annoyance rather than a problem but I would very much like to understand the semantics here.
All I want to do is to run an arbitrary command on a temporary command-prompt session which itself running under a bash session.
My success rate is 50/50 as some command works as expected whereas others not so much.
I think the problem may lie around arguments not lining up properly (i.e. missing or merged arguments)
I'll try to explain what I mean by weird by a series of commands and responses. (I'm trying to get the word test to be printed on the screen.)
I'm running these under GNU bash, version 3.1.0(1)-release (i686-pc-msys) Bundled with Git-1.8.4:
First attempt:
$ cmd /c echo test
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
c:\>
Second attempt:
$ cmd '/c echo test'
test"
Third attempt:
$ cmd "/c echo test"
test"
Fourth attempt:
$ cmd /c\ echo\ test
test"
Fifth attempt:
$ cmd "/c echo" test
'echo" test' is not recognized as an internal or external command,
operable program or batch file.
I'd really appreciate any pointers or insights into the behaviors above as this is unintuitional to me and driving me crazy!
Edit:
There is another question that appears similar to this, but it really isn't, mainly because it's about running batch files through CMD /C that doesn't require any arguments.
It doesn't really answer my question about how to provide arguments properly to windows command line apps, and even though the examples are about CMD /C, the answer here can be applied to many other Windows command line apps as well.
This is actually documented in the ReleaseNotes file (in the top level folder of your installed Git for Windows)
Also, extra care has to be paid to pass Windows programs Windows paths, as they have no clue about MSys style POSIX paths -- You can use something like $(cmd //c echo "$POSIXPATH").
If you use cmd //c echo test it works as expected.
$ cmd //c echo test
test
The cause is to do with trying to ensure that posix paths end up being passed to the git utilities properly. For this reason, Git for Windows includes a modified MSYS layer that affects command arguments. You should note that it is not intended that the bash shell and tools provided with Git for Windows be used as general purpose unix tools for Windows. If you want a general purpose unix-style toolset then you should install MSYS or cygwin. The Git Bash shell is setup for working with git and sometimes that shows.
After reading this article I've found solution which works for me:
$ cat gvim.sh
cmd << EOD
gvim $#
EOD
$
Windows 8.1, Git (version 1.9.5-preview20141217), GNU bash, version 3.1.20(4)-release (i686-pc-msys).
I am able to mostly reproduce the problem using gnu bash for Windows.
I can't quite establish a pattern with the first form without any quotes. It seems to work with the Windows ECHO command, but not with other commands like DIR. EDIT - It turns out gnu bash is putting quotes around my command, so echo test becomes "echo" "test". The quotes cause cmd.exe to look for an external command instead of the internal ECHO command. I happen to have "echo.exe", so it appears to run. The odd thing is the quotes around test are not displayed. When I attempt to run the DIR command, it fails entirely because there isn't any DIR.EXE.
The subsequent forms with quotes (except the last one), or escaped spaces, work the same as you are seeing - there is an unwanted trailing quote in the command.
I could not come up with a clean solution. However, I have an ugly hack that should give you the desired result. Simply concatenate a REM command at the end of your command. The REM will comment out the unwanted trailing quote. It is important that there be a space after REM, otherwise REM" will not be recognized as a valid command. Any of the following should work.
$ cmd '/c echo test&rem '
$ cmd "/c echo test&rem "
$ cmd /c\ echo\ test\&rem\
Note that the last command has a space after the backslash.
The technique should work for pretty much any command string that you might want to execute via CMD.EXE.
As I explain here, there is an additional alternative when using modern Git for Windows' bash
MSYS_NO_PATHCONV=1 cmd /c echo test
Explanation of each attempt
TL;DR
The unfortunate answer is in Windows, there are many ways arguments can be parsed, and you have to format your output in bash in such a way that is will be reparsed by the windows program it the way it expects
Second, third, and forth attempts are all actually identical
This is the same as (in cmd) > cmd "/c echo test". Windows cmd only uses " quotes, so somewhere between the runtimes, your (in bash) $ cmd '/c echo test' converts all the arguments to "/c echo test" which is happily parses.
Since from a bash point of view, the 2nd/3rd/4th attempt are all the same, they all give the same response. The surprise " is due to how windows parsing only using " and not ', hence it is the same as > cmd "/c echo test"
Fifth attempt
$ cmd "/c echo" test is the same as > cmd /c echo" test. (I'm guessing: The space after the /c is optional, so cmd isn't confused by /c echo being the space being literal due to the first quote.) So it is trying to execute the command echo" test which doesn't exist. The space is interpreted as literal because of the quote. Likewise, if you had done $ cmd "/c echo "test you would get the output "test, because the space is no longer being treats as literal, and no longer part of the command echo
Note: > cmd "/c echo" test and > cmd /c echo" test error the same. My guess here is that cmd parsed everything after the /c on its own, so the initial " has no affect, as parsing starts all over again. Chalk that up to special cmd weirdness.
This can actually be reproduced using python for windows, which has nothing to do with msys/mingw/git/bash/glibc/etc...
python -c "import subprocess; subprocess.Popen(['cmd', '/c echo test'])"
I noticed git-bash treats the /c argument like a C: drive:
C:\Windows\system32\cmd.exe C:/ echo test
As dbenham found double quotes are added. This echos test" for example:
cmd /c\ echo\ test
I needed the same line (script) to work in git-bash as well as Cygwin Bash. The only ways that work are
cmd /c\ echo\ test\&rem\
(note that this line needs to end in a space), and
cmd << EOC
echo test
EOC
So escape every space after the /c and add \&rem\  at the end of the line (including the trailing space), or just wrap the command in a here document.
All this probably depends on the version of git-bash and the specific commands. :-(
Because you mention that you're using the Git for Windows bundle, I figured I'd point out that it includes winpty, which seems to be quite readable.
$ winpty echo test
test
$ site="Default Web Site"
$ winpty 'C:\Windows\System32\inetsrv\appcmd' list site "${site}" /text:ID
1
This seems to work under 1.9.5.msysgit.1
!foo=`bar`
cmd //c \\\\unc-path\\with\\slashes -args \"Quoted Arguments $foo\"

cmd.exe vs. cmd output

I am running Cygwin on Windows and occasionally I need to call cmd.exe from Bash.
However I have noticed that I am getting different results depending on how I call it
$ cmd.exe /c 'echo "http://asdf.com?a=1&b=2"'
"http://asdf.com?a=1&b=2"
$ cmd /c 'echo "http://asdf.com?a=1&b=2"'
\"http://asdf.com?a=1&b=2\"
Why is this happening?
I noticed if you call it like this it is closer to the cmd.exe output. Adding a space inside the quotes changes the output.
$ cmd /c echo 'http://asdf.com?a=1&b=2 '
"http://asdf.com?a=1&b=2 "
More information
one or more whitespace characters between the the two quote characters
CMD.exe (Command Shell)

What is `cmd /s` for?

The Windows command prompt (cmd.exe) has an optional /s parameter, which modifies the behavior of /c (run a particular command and then exit) or /k (run a particular command and then show a shell prompt). This /s parameter evidently has something to do with some arcane quote handling.
The docs are confusing, but as far as I can tell, when you do cmd /csomething, and the something contains quotation marks, then by default cmd will sometimes strip off those quotes, and /s tells it to leave them alone.
What I don't understand is when the quote removal would break anything, because that's the only time /s ("suppress the default quote-removal behavior") would be necessary. It only removes quotes under a certain arcane set of conditions, and one of those conditions is that the first character after the /c must be a quotation mark. So it's not removing quotes around arguments; it's either removing quotes around the path to the EXE you're running, or around the entire command line (or possibly around the first half of the command line, which would be bizarre).
If the path to the EXE is quoted, e.g. cmd /c "c:\tools\foo.exe" arg1 arg2, then quotes are unnecessary, and if cmd wants to remove them, fine. (It won't remove them if the path has a space in the name -- that's another of the arcane rules.) I can't imagine any reason to suppress the quote removal, so /s seems unnecessary.
If the entire command line is quoted, e.g. cmd /c "foo.exe arg1 arg2", then it seems like quote removal would be a necessity, since there's no EXE named foo.exe arg1 arg2 on the system; so it seems like opting out of quote removal using /s would actually break things. (In actual fact, however, it does not break things: cmd /s /c "foo.exe arg1 arg2" works just fine.)
Is there some subtlety to /s that's eluding me? When would it ever be necessary? When would it even make any difference?
Cmd /S is very useful as it saves you having to worry about "quoting quotes". Recall that the /C argument means "execute this command as if I had typed it at the prompt, then quit".
So if you have a complicated command which you want to pass to CMD.exe you either have to remember CMD's argument quoting rules, and properly escape all of the quotes, or use /S, which triggers a special non-parsing rule of "Strip first and last " and treat all other characters as the command to execute unchanged".
You would use it where you want to take advantage of the capabilities of the CMD shell, rather than directly calling another program. For example environment variable expansion, output or input redirection, or using CMD.exe built-ins.
Example:
Use a shell built-in: This executes as-if you had typed DEL /Q/S "%TMP%\TestFile" at the prompt:
CMD.exe /S /C " DEL /Q/S "%TMP%\TestFile" "
This executes SomeCommand.exe redirecting standard output to a temp file and standard error to the same place:
CMD.exe /S /C " "%UserProfile%\SomeCommand.exe" > "%TMP%\TestOutput.txt" 2>&1 "
So what does /S give you extra? Mainly it saves you from having to worry about quoting the quotes. It also helps where you are unsure whether for example an environtment variable contains quote characters. Just say /S and put an extra quote at the beginning and end.
Vaguely Related: $* in Bourne Shell.
Some background
Recall that the list of arguments to main() is a C-ism and Unix-ism. The Unix/Linux shell (e.g. Bourne Shell etc) interprets the command line, un-quotes the arguments, expands wildcards like * to lists of files, and passes a list of arguments to the called program.
So if you say:
$ vi *.txt
The vi command sees for example these arguments:
vi
a.txt
b.txt
c.txt
d.txt
This is because unix/linux operates internally on the basis of "list of arguments".
Windows, which derives ultimately from CP/M and VAX, does not use this system internally. To the operating system, the command line is just a single string of characters. It is the responsibility of the called program to interpret the command line, expand file globs (* etc) and deal with unquoting quoted arguments.
So the arguments expected by C, have to be hacked up by the C runtime library. The operating system only supplies a single string with the arguments in, and if your language is not C (or even if it is) it may not be interpreted as space-separated arguments quoted according to shell rules, but as something completely different.
Here's an example of how it can make a difference.
Suppose you have two executables: c:\Program.exe and c:\Program Files\foo.exe.
If you say
cmd /c "c:\Program Files\foo"
you'll run foo.exe (with no arguments) whereas if you say
cmd /s /c "c:\Program Files\foo"
you'll run Program.exe with Files\foo as the argument.
(Oddly enough, in the first example, if foo.exe didn't exist, Program.exe would run instead.)
Addendum: if you were to type
c:\Program Files\foo
at the command prompt, you would run Program.exe (as happens with cmd /s /c) rather than foo.exe (as happens with just cmd /c). So one reason for using /s would be if you want to make sure a command is parsed in exactly the same way as if it were being typed at the command prompt. This is probably more likely to be desirable in the scenario in the question Michael Burr linked to, where cmd.exe is being launched by CreateProcess rather than from a batch file or the command line itself..
That is, if you say
CreateProcess("cmd.exe", "cmd /s /c \"" MY_COMMAND "\"", ...)
then the string MY_COMMAND will be parsed exactly as if it were typed at the command prompt. If you're taking command-line input from the user, or if you're a library processing a command line provided by an application, that's probably a good idea. For example, the C runtime library system() function might be implemented in this way.
In all but one specific case, the /S won't actually make any difference.
The help for cmd.exe is accurate, if a bit complicated:
If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:
If all of the following conditions are met, then quote characters
on the command line are preserved:
no /S switch
exactly two quote characters
no special characters between the two quote characters,
where special is one of: &<>()#^|
there are one or more whitespace characters between the
two quote characters
the string between the two quote characters is the name
of an executable file.
Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.
I'd summarize as follows:
Normal behavior:
If the rest of the command line after /K or /C starts with a quote, both that quote and the final quote are removed. (See exception below.) Other than that, no quotes are removed.
Exception:
If the rest of the command line after /K or /C starts with a quote, followed by the name of an executable file, followed by another quote, AND if those are the only two quotes, AND if the file name contains spaces but contains no special characters, then the quotes are not removed (even though they normally would have been removed according to the rule above).
The only effect of /S is to override this one exception, so that the two quote characters are still removed in that case.
If you always use /S, you can forget about the exception and just remember the "normal" case. The downside is that cmd.exe /S /C "file name with spaces.exe" argument1 won't work without adding an extra set of quotes, whereas without /S it would have worked... until you decide to replace argument1 with "argument1".

How do I run two commands in one line in Windows CMD?

I want to run two commands in a Windows CMD console.
In Linux I would do it like this
touch thisfile ; ls -lstrh
How is it done on Windows?
Like this on all Microsoft OSes since 2000, and still good today:
dir & echo foo
If you want the second command to execute only if the first exited successfully:
dir && echo foo
The single ampersand (&) syntax to execute multiple commands on one line goes back to Windows XP, Windows 2000, and some earlier NT versions. (4.0 at least, according to one commenter here.)
There are quite a few other points about this that you'll find scrolling down this page.
Historical data follows, for those who may find it educational.
Prior to that, the && syntax was only a feature of the shell replacement 4DOS before that feature was added to the Microsoft command interpreter.
In Windows 95, 98 and ME, you'd use the pipe character instead:
dir | echo foo
In MS-DOS 5.0 and later, through some earlier Windows and NT versions of the command interpreter, the (undocumented) command separator was character 20 (Ctrl+T) which I'll represent with ^T here.
dir ^T echo foo
A quote from the documentation:
Source: Microsoft, Windows XP Professional Product Documentation, Command shell overview
Also: An A-Z Index of Windows CMD commands
Using multiple commands and conditional processing symbols
You can run multiple commands from a single command line or script using conditional processing symbols. When you run multiple commands with conditional processing symbols, the commands to the right of the conditional processing symbol act based upon the results of the command to the left of the conditional processing symbol.
For example, you might want to run a command only if the previous command fails. Or, you might want to run a command only if the previous command is successful.
You can use the special characters listed in the following table to pass multiple commands.
& [...]
command1 & command2
Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command.
&& [...]
command1 && command2
Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.
|| [...]
command1 || command2
Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).
( ) [...]
(command1 & command2)
Use to group or nest multiple commands.
; or ,
command1 parameter1;parameter2
Use to separate command parameters.
& is the Bash equivalent for ; ( run commands) and && is the Bash equivalent of && (run commands only when the previous has not caused an error).
If you want to create a cmd shortcut (for example on your desktop) add /k parameter (/k means keep, /c will close window):
cmd /k echo hello && cd c:\ && cd Windows
You can use & to run commands one after another. Example: c:\dir & vim myFile.txt
You can use call to overcome the problem of environment variables being evaluated too soon - e.g.
set A=Hello & call echo %A%
A number of processing symbols can be used when running several commands on the same line, and may lead to processing redirection in some cases, altering output in other case, or just fail. One important case is placing on the same line commands that manipulate variables.
#echo off
setlocal enabledelayedexpansion
set count=0
set "count=1" & echo %count% !count!
0 1
As you see in the above example, when commands using variables are placed on the same line, you must use delayed expansion to update your variable values. If your variable is indexed, use CALL command with %% modifiers to update its value on the same line:
set "i=5" & set "arg!i!=MyFile!i!" & call echo path!i!=%temp%\%%arg!i!%%
path5=C:\Users\UserName\AppData\Local\Temp\MyFile5
cmd /c ipconfig /all & Output.txt
This command execute command and open Output.txt file in a single command
So, I was trying to enable the specific task of running RegAsm (register assembly) from a context menu. The issue I had was that the result would flash up and go away before I could read it. So I tried piping to Pause, which does not work when the command fails (as mentioned here Pause command not working in .bat script and here Batch file command PAUSE does not work). So I tried cmd /k but that leaves the window open for more commands (I just want to read the result). So I added a pause followed by exit to the chain, resulting in the following:
cmd /k C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe "%1" /codebase \"%1\" & pause & exit
This works like a charm -- RegAsm runs on the file and shows its results, then a "Press any key to continue..." prompt is shown, then the command prompt window closes when a key is pressed.
P.S. For others who might be interested, you can use the following .reg file entries to add a dllfile association to .dll files and then a RegAsm command extension to that (notice the escaped quotes and backslashes):
[HKEY_CLASSES_ROOT\.dll]
"Content Type"="application/x-msdownload"
#="dllfile"
[HKEY_CLASSES_ROOT\dllfile]
#="Application Extension"
[HKEY_CLASSES_ROOT\dllfile\Shell\RegAsm]
#="Register Assembly"
[HKEY_CLASSES_ROOT\dllfile\Shell\RegAsm\command]
#="cmd /k C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\regasm.exe \"%1\" /codebase \"%1\" & pause & exit"
Now I have a nice right-click menu to register an assembly.
In windows, I used all the above solutions &, && but nothing worked
Finally ';' symbol worked for me
npm install; npm start
Well, you have two options: Piping, or just &:
DIR /S & START FILE.TXT
Or,
tasklist | find "notepad.exe"
Piping (|) is more for taking the output of one command, and putting it into another. And (&) is just saying run this, and that.
In order to execute two commands at the same time, you must put an & (ampersand) symbol between the two commands. Like so:
color 0a & start chrome.exe
Cheers!
I try to have two pings in the same window, and it is a serial command on the same line. After finishing the first, run the second command.
The solution was to combine with start /b on a Windows 7 command prompt.
Start as usual, without /b, and launch in a separate window.
The command used to launch in the same line is:
start /b command1 parameters & command2 parameters
Any way, if you wish to parse the output, I don't recommend to use this.
I noticed the output is scrambled between the output of the commands.
Use & symbol in windows to use command in one line
C:\Users\Arshdeep Singh>cd Desktop\PROJECTS\PYTHON\programiz & jupyter notebook
like in linux
we use,
touch thisfile ; ls -lstrh
I was trying to create batch file to start elevated cmd and to make it run 2 separate commands.
When I used & or && characters, I got a problem. For instance, this is the text in my batch file:
powershell.exe -Command "Start-Process cmd \"/k echo hello && call cd C:\ \" -Verb RunAs"
I get parse error:
After several guesses I found out, that if you surround && with quotes like "&&" it works:
powershell.exe -Command "Start-Process cmd \"/k echo hello "&&" call cd C:\ \" -Verb RunAs"
And here's the result:
May be this'll help someone :)
No, cd / && tree && echo %time%. The time echoed is at when the first command is executed.
The piping has some issue, but it is not critical as long as people know how it works.
One more example: For example, when we use the gulp build system, instead of
gulp - default > build
gulp build - build build-folder
gulp watch - start file-watch
gulp dist - build dist-folder
We can do that with one line:
cd c:\xampp\htdocs\project & gulp & gulp watch
Yes there is. It's &.
&& will execute command 2 when command 1 is complete providing it didn't fail.
& will execute regardless.
With windows 10 you can also use scriptrunner:
ScriptRunner.exe -appvscript demoA.cmd arg1 arg2 -appvscriptrunnerparameters -wait -timeout=30 -rollbackonerror -appvscript demoB.ps1 arg3 arg4 -appvscriptrunnerparameters -wait -timeout=30 -rollbackonerror
it allows you to start few commands on one line you want you can run them consecutive or without waiting each other, you can put timeouts and rollback on error.
Try to create a .bat ot .cmd file with those lines using doskey key and $T which is equivalent to & to do several command line in just one line :
touch=echo off $T echo. ^> $* $T dir /B $T echo on
It'll create an empty file.
Example:
touch myfile
In cmd you'll get something like this:
But as mentioned previously by others, it is really advised to use & operator to do many command line in one line from CMD prompt.
Enjoy =)
When you try to use or manipulate variables in one line beware of their content! E.g. a variable like the following
PATH=C:\Program Files (x86)\somewhere;"C:\Company\Cool Tool";%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;
may lead to a lot of unhand-able trouble if you use it as %PATH%
The closing parentheses terminate your group statement
The double quotes don't allow you to use %PATH% to handle the parentheses problem
And what will a referenced variable like %USERPROFILE% contain?
It's simple: just differentiate them with && signs.
Example:
echo "Hello World" && echo "GoodBye World".
"Goodbye World" will be printed after "Hello World".

Resources