How do we extend a command to the next line?
Basically what's the Windows alternative for Linux's:
ls -l \
/usr/
Here we use backslashes to extend the command onto the next lines.
What's the equivalent for Windows?
How do we extend a command to the next line?
Basically what's the Windows alternative for Linux's:
ls -l \
/usr/
Here we use backslashes to extend the command onto the next lines.
What's the equivalent for Windows?
Solution 1 In CMD, Use ^
, example
docker run -dp 3000:3000 ^
-w /app -v "$(pwd):/app" ^
--network todo-app ^
-e MYSQL_HOST=mysql ^
-e MYSQL_USER=root ^
-e MYSQL_PASSWORD=secret ^
-e MYSQL_DB=todos ^
node:12-alpine ^
cmd "npm install && yarn run start"
Solution 2 In PowerShell, Use ` (backtick) , Example
docker run -dp 3000:3000 `
-w /app -v "$(pwd):/app" `
--network todo-app `
-e MYSQL_HOST=mysql `
-e MYSQL_USER=root `
-e MYSQL_PASSWORD=secret `
-e MYSQL_DB=todos `
node:12-alpine `
cmd "npm install && npm run start"
After trying almost every key on my keyboard:
C:\Users\Tim>cd ^
Mehr? Desktop
C:\Users\Tim\Desktop>
So it seems to be the ^ key.
Note that ^
trailing works in batch files as well (tested on Windows 7).
In the Windows Command Prompt the ^
is used to escape the next character on the command line. (Like \
is used in strings.) Characters that need to be used in the command line as they are should have a ^ prefixed to them, hence that's why it works for the newline.
For reference the characters that need escaping (if specified as command arguments and not within quotes) are: &|()
So the equivalent of your linux example would be (the More? being a prompt):
C:\> dir ^
More? C:\Windows
Solution 1 In CMD, Use ^
, example
docker run -dp 3000:3000 ^
-w /app -v "$(pwd):/app" ^
--network todo-app ^
-e MYSQL_HOST=mysql ^
-e MYSQL_USER=root ^
-e MYSQL_PASSWORD=secret ^
-e MYSQL_DB=todos ^
node:12-alpine ^
cmd "npm install && yarn run start"
Solution 2 In PowerShell, Use ` (backtick) , Example
docker run -dp 3000:3000 `
-w /app -v "$(pwd):/app" `
--network todo-app `
-e MYSQL_HOST=mysql `
-e MYSQL_USER=root `
-e MYSQL_PASSWORD=secret `
-e MYSQL_DB=todos `
node:12-alpine `
cmd "npm install && npm run start"
The caret character works, however the next line should not start with double quotes. e.g. this will not work:
C:\ ^
"SampleText" ..
Start next line without double quotes (not a valid example, just to illustrate)
If you came here looking for an answer to this question but not exactly the way the OP meant, ie how do you get multi-line CMD to work in a single line, I have a sort of dangerous answer for you.
Trying to use this with things that actually use piping, like say findstr
is quite problematic. The same goes for dealing with else
s. But if you just want a multi-line conditional command to execute directly from CMD and not via a batch file, this should work well.
Let's say you have something like this in a batch that you want to run directly in command prompt:
@echo off
for /r %%T IN (*.*) DO (
if /i "%%~xT"==".sln" (
echo "%%~T" is a normal SLN file, and not a .SLN.METAPROJ or .SLN.PROJ file
echo Dumping SLN file contents
type "%%~T"
)
)
Now, you could use the line-continuation carat (^
) and manually type it out like this, but warning, it's tedious and if you mess up you can learn the joy of typing it all out again.
Well, it won't work with just ^
thanks to escaping mechanisms inside of parentheses shrug At least not as-written. You actually would need to double up the carats like so:
@echo off ^
More? for /r %T IN (*.sln) DO (^^
More? if /i "%~xT"==".sln" (^^
More? echo "%~T" is a normal SLN file, and not a .SLN.METAPROJ or .SLN.PROJ file^^
More? echo Dumping SLN file contents^^
More? type "%~T"))
Instead, you can be a dirty sneaky scripter from the wrong side of the tracks that don't need no carats by swapping them out for a single pipe (|
) per continuation of a loop/expression:
@echo off
for /r %T IN (*.sln) DO if /i "%~xT"==".sln" echo "%~T" is a normal SLN file, and not a .SLN.METAPROJ or .SLN.PROJ file | echo Dumping SLN file contents | type "%~T"
In windows 10 22H2, simply press enter
after an open parenthesis gives you same result as typing ^
at the end of line.
For example, after typing this line of code:
>for %f in (*) do (
press enter
and a new line like this will pop up automatically:
More?
You can even split string quoted parameters. End the line with "^
and start the next line with "
. E.g.
dir "\users"
can be split as:
dir "\use"^
"rs"
© 2022 - 2024 — McMap. All rights reserved.