Use this skill whenever the user is on Windows and asks for terminal commands, shell scripts, escaping rules, or asks to compare Windows and Linux terminal syntax.
Do not say "Windows terminal syntax" as if Windows had one shell.
On Windows, distinguish between:
powershell, pwsh)cmd)Windows Terminal is only the terminal app. It can host multiple shells.
If the shell is unclear:
PowerShell: and cmd: variants.Never give Bash-only syntax to a Windows user unless they explicitly said they are using:
Examples of Bash-only habits that must not be handed to cmd/PowerShell users without labeling:
export NAME=value$VAR for environment variables in cmd\" as the default Windows answerrm, mv, cp as if they were native cmd syntax\&& / || compatibility ruleDo not say "Windows does not support &&".
The real compatibility matrix is:
&, &&, and ||&& and ||&& and ||&& and ||This means the question is which shell and which PowerShell version, not just "Windows or not".
If you know only that the user is in PowerShell, but do not know whether it is:
powershell), orpwsh / modern PowerShell)then avoid && / || in the default answer and use explicit guarded logic instead.
^& | ( ) < > ^^""..." for paths with spaces%USERPROFILE%^ at end of line&&&||'...' for literal strings"..." are expandable: variables/interpolation work inside them ``$env:USERPROFILE&& / ||&& / ||if checks over pipeline chain operators\'...'"..."$HOME, $VAR\ at end of lineWhen the user asks for a command and the shell is not specified, prefer this structure:
PowerShell:
<command>
cmd:
<command>
Do not provide only one unlabeled command if syntax differs.
If version compatibility matters, prefer this structure:
PowerShell 5.1 compatible:
<command>
PowerShell 7+:
<command>
cmd:
<command>
If you were about to write Bash chaining, rewrite for the actual shell.
cmd1 && cmd2cmd1 && cmd2cmd1 && cmd2cmd1; if ($?) { cmd2 }Example:
PowerShell 5.1 compatible:
cmd1
if ($?) { cmd2 }
PowerShell 7+:
cmd1 && cmd2
cmd:
cmd1 && cmd2
&&When a user says && does not work, first infer the likely shell:
cmd1 && cmd2 → cmd1; if ($?) { cmd2 }cmd1 || cmd2 → cmd1; if (-not $?) { cmd2 }&& / ||&& / || are valid and usually can stay as-isFor native executables in PowerShell, a more explicit form may be safer when exit-code behavior matters:
cmd1
if ($LASTEXITCODE -eq 0) { cmd2 }
Use this especially when the left-hand side is an .exe or external CLI and you want to be precise about process exit codes.
export NAME=value$env:NAME = 'value'set NAME=valueFor reading values:
$NAME$env:NAME%NAME%Always quote Windows paths with spaces.
"C:\Program Files\App""C:\Program Files\App"If the user is in cmd, remember ^ escaping for metacharacters and quote-sensitive cases.
If the user is in PowerShell, prefer changing quote style instead of piling on escapes.
Example:
PowerShell:
python -c 'print("hello")'
cmd:
python -c "print(^\"hello^\")"
Keep cmd examples minimal and tested-looking; cmd quoting gets messy fast.
Prefer shell-native multi-line styles:
PowerShell:
& python `
script.py `
--name test
cmd:
python ^
script.py ^
--name test
But if a one-line command is acceptable, prefer one line.
For cmd and PowerShell, deeply nested quoting can become fragile fast, especially for commands like:
python -c "..."If quoting starts getting ugly, prefer a safer rewrite instead of trying to perfectly escape everything.
Preferred escape hatches:
Rule of thumb: if the example needs a paragraph to explain the escaping, rewrite the approach.
Windows shells differ a lot in redirection and pipeline behavior.
If a command depends on tricky redirection, prefer a simpler pattern:
Rule of thumb: if correctness depends on subtle pipe/redirection semantics, choose a more explicit multi-step form.
Use this section when the user pastes a Bash command and wants the Windows equivalent.
cmd1 && cmd2Bash:
cmd1 && cmd2
PowerShell 5.1 compatible:
cmd1
if ($?) { cmd2 }
PowerShell 7+:
cmd1 && cmd2
cmd:
cmd1 && cmd2
cmd1 || cmd2Bash:
cmd1 || cmd2
PowerShell 5.1 compatible:
cmd1
if (-not $?) { cmd2 }
PowerShell 7+:
cmd1 || cmd2
cmd:
cmd1 || cmd2
export NAME=valueBash:
export NAME=value
PowerShell:
$env:NAME = 'value'
cmd:
set NAME=value
echo $NAMEBash:
echo $NAME
PowerShell:
echo $env:NAME
cmd:
echo %NAME%
~/file.txtBash:
cat ~/file.txt
PowerShell:
Get-Content $HOME\file.txt
cmd:
type %USERPROFILE%\file.txt
rm, cp, mvBash:
rm file.txt
cp a.txt b.txt
mv a.txt b.txt
PowerShell:
Remove-Item file.txt
Copy-Item a.txt b.txt
Move-Item a.txt b.txt
cmd:
del file.txt
copy a.txt b.txt
move a.txt b.txt
VAR=value commandThis Bash pattern has no direct same-shape equivalent in PowerShell or cmd.
Bash:
NAME=value mytool
PowerShell:
$env:NAME = 'value'
mytool
cmd:
set NAME=value
mytool
If the temporary-only behavior matters, mention that Bash can scope env vars to one command, while PowerShell/cmd examples usually need extra cleanup if true one-command scope is required.
--flag value`
Bash:
command \
--flag value
PowerShell:
command `
--flag value
cmd:
command ^
--flag value
grep pattern file.txtBash:
grep pattern file.txt
PowerShell:
Select-String pattern file.txt
cmd:
findstr pattern file.txt
pwd and lsBash:
pwd
ls
PowerShell:
Get-Location
Get-ChildItem
cmd:
cd
dir
Note: ls and pwd may appear to work in PowerShell because of aliases, but if you are teaching shell syntax clearly, prefer the native command names.
Use these heuristics when deciding whether && is safe in PowerShell:
shell=powershell, that only tells you the shell family, not always the exact versionpwsh usually means PowerShell 7+powershell often means Windows PowerShell 5.1 on older/default Windows setupsIf you need a detection command, give:
PowerShell:
$PSVersionTable.PSVersion
When replying to Windows users:
shell=powershell.cmd or says ^", answer in cmd syntax.&& is involved, distinguish cmd, PowerShell 7+, and PowerShell 5.1 instead of treating all Windows shells the same.Use short phrasing like:
Windows Terminal is just the terminal app; the real syntax depends on whether you're running PowerShell or cmd.cmd, escape special characters with ^.&& works in cmd and PowerShell 7+, but not in Windows PowerShell 5.1.cmd commonly uses ^ for escaping special characters.cmd1; if ($?) { cmd2 } is safer than assuming && support.共 1 个版本