difficult starting cmd prompt with execution of two commands

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
taripo
Posts: 227
Joined: 01 Aug 2011 13:48

difficult starting cmd prompt with execution of two commands

#1 Post by taripo » 20 May 2018 07:21

I am having difficulty starting cmd prompt with execution of two commands



this works fine (though is just for one command)

Code: Select all

C:\>start cmd /k echo abc

but this, with two commands, fails.. it closes immediately.

Code: Select all

C:\>start cmd /k (echo abc & echo def)
I tried adding a pause to get a better grasp of what was happening, but then I got another error

Code: Select all

C:\>start cmd /k (echo abc & echo def & pause)
def
'pause)' is not recognized as an internal or external command,operable program or batch file.

C:\>start cmd /k ((echo abc) & (echo def) & (pause))
) was unexpected at this time.

C:\>
So I can't really see what's going on and how to do it

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: difficult starting cmd prompt with execution of two commands

#2 Post by elzooilogico » 20 May 2018 07:52

Code: Select all

start "" cmd /k "bla & bla & bla"
or

Code: Select all

start "" cmd "/k "bla &bla & bla""
cyquotation makes quite a difference. first quotes are window title

sst
Posts: 93
Joined: 12 Apr 2018 23:45

Re: difficult starting cmd prompt with execution of two commands

#3 Post by sst » 20 May 2018 08:48

You know that & is command splitter but forgot that this line

Code: Select all

start cmd /k (echo abc & echo def)
will be parsed and splitted in to two separate commands before the second cmd instance have a chance to grab it as command line.
your line will basically will be converted to this before execution

Code: Select all

start cmd /k (echo abc
echo def)
The first line starts a new instance of cmd passing (echo abc as the command to execute but it's syntax is invalid because of the unbalanced parentheses, the new cmd instance will not complain about it but it will be terminated immediately then the second line echo def) will be executed which will output def)

For this to work you have two options:
1. Put the the part that is intended to be passed to the second cmd instance in quotes

Code: Select all

start cmd /k "(echo abc & echo def)"
2. Or escape the special characters to prevent them to be parsed by parent cmd

Code: Select all

start cmd /k ^(echo abc ^& echo def^)
But in your particular test case there is no need to use parentheses at all so it can be simplified like this

Code: Select all

start cmd /k "echo abc & echo def"
start cmd /k echo abc ^& echo def

taripo
Posts: 227
Joined: 01 Aug 2011 13:48

Re: difficult starting cmd prompt with execution of two commands

#4 Post by taripo » 24 May 2018 16:47

@sst, thanks, you're spot on, I hadn't realised that the round brackets / parentheses , when occurring in a parameter, don't group things! (is that right?)
You solved it though. I like both those solutions, the quotes one, and the escaping the ampersand one.

you explained that very well with how the parameters are broken down.. I tested it and indeed

(w.c and w2.c referenced at bottom of post)

Code: Select all

C:\>w cmd /k echo abc & echo def
argv[0] = w
argv[1] = cmd
argv[2] = /k
argv[3] = echo
argv[4] = abc
def

C:\>w cmd /k echo (abc & echo def)
argv[0] = w
argv[1] = cmd
argv[2] = /k
argv[3] = echo
argv[4] = (abc
def)

C:\>

C:\>w cmd /k echo abc ^& echo def
argv[0] = w
argv[1] = cmd
argv[2] = /k
argv[3] = echo
argv[4] = abc
argv[5] = &
argv[6] = echo
argv[7] = def

So I see the importance of the quotes there.. or the escaping the ampersand

Funnily enough this doesn't work

C:\>start cmd /k echo abc "&" echo def

even though the argsv come out the same

Code: Select all

C:\>w cmd /k echo abc "&" echo def
argv[0] = w
argv[1] = cmd
argv[2] = /k
argv[3] = echo
argv[4] = abc
argv[5] = &
argv[6] = echo
argv[7] = def
Though the command line is different for them,

C:\>w2 start cmd /k echo abc "&" echo def
w2 start cmd /k echo abc "&" echo def

C:\>w2 start cmd /k echo abc ^& echo def
w2 start cmd /k echo abc & echo def
C:\>



so maybe start uses some kind of mixture of first getting the command line then splitting into arguments

@elz , well spotted re quotes around the echo, though I think the start "" is a bit of a red herring here.. it's not necessary. It is necessary when the path to the executable contains a space. e.g. C:\>start c:\crp\zzzza.bat works, but C:\>start "c:\crp\bab zab\fafa.bat" fails and requires C:\>start "" "c:\crp\bab zab\fafa.bat" So there are cases when you need the "" but not here.

@sst could solution and explanation.

Thanks guys

* w.c and w2.c https://pastebin.com/raw/KTWihmPB

sst
Posts: 93
Joined: 12 Apr 2018 23:45

Re: difficult starting cmd prompt with execution of two commands

#5 Post by sst » 26 May 2018 18:58

taripo wrote:
24 May 2018 16:47
Funnily enough this doesn't work

C:\>start cmd /k echo abc "&" echo def

even though the argsv come out the same

Code: Select all

C:\>w cmd /k echo abc "&" echo def
argv[0] = w
argv[1] = cmd
argv[2] = /k
argv[3] = echo
argv[4] = abc
argv[5] = &
argv[6] = echo
argv[7] = def
Though the command line is different for them,

C:\>w2 start cmd /k echo abc "&" echo def
w2 start cmd /k echo abc "&" echo def

C:\>w2 start cmd /k echo abc ^& echo def
w2 start cmd /k echo abc & echo def
C:\>
taripo wrote:
24 May 2018 16:47
so maybe start uses some kind of mixture of first getting the command line then splitting into arguments
start does not touch command line of the target command and will pass it as is.

That is the effect of C-Runtime's command line processor in your w program which will split it in to separate argv elements and unquotes each of them, cmd on the other hand, accesses raw command line and uses it's own parser for processing it rather than using argv.

And everything that comes after /K or /C will be parsed and processed as if it was typed at cmd's command prompt. Except when certain conditions are met, cmd will always unquote this part first then continues with rest of the processing. (To see when and how cmd will keep quotes after /K or /C refer to cmd /?)

So with the following:

Code: Select all

C:\>start cmd /k echo abc "&" echo def
the second cmd will see this as the command to execute:

Code: Select all

echo abc "&" echo def
which will output:
abc "&" echo def
And with this:

Code: Select all

C:\>start cmd /k "echo abc & echo def"
The second cmd will see this as the command to execute:

Code: Select all

"echo abc & echo def"
Unquotes it:

Code: Select all

echo abc & echo def
Then executes which will output:
abc
def

Post Reply