A single shell snippet that works in Bash, PowerShell and Command Prompt.
echo ^">NUL \" >$null "'&powershell ";iex (iwr __WINDOWS_SCRIPT_URL__);echo >$null '>/dev/null;curl -s __LINUX_OR_MACOS_SCRIPT_URL__|sh;echo >/dev/null \'
- on Windows1 downloads and runs
windows_code.ps1
. - on Linux/macOS2 downloads and runs
linux_or_macos_code.sh
.
1. Note there exists a cross-platform PowerShell nowadays, so technically speaking you should still do an OS check there - but realistically speaking, who uses that anyway?
2. ... or any OS that runs a bash-like shell (yeah it could even be Windows through Git Bash or something)
echo ^">NUL \" >$null "'&powershell ";iex (iwr https://raw.githubusercontent.com/kwyntes/crossshell/main/windows_code.ps1);echo >$null '>/dev/null;curl -s https://raw.githubusercontent.com/kwyntes/crossshell/main/linux_or_macos_code.sh|sh;echo >/dev/null \'
echo ^">NUL \" >$null "'&powershell ";iex (iwr https://raw.githubusercontent.com/kwyntes/crossshell/main/windows_code.ps1);echo >$null '>/dev/null;curl -s https://raw.githubusercontent.com/kwyntes/crossshell/main/linux_or_macos_code.sh|sh;echo >/dev/null \'
echo ^">NUL \" >$null "'&powershell ";iex (iwr https://raw.githubusercontent.com/kwyntes/crossshell/main/windows_code.ps1);echo >$null '>/dev/null;curl -s https://raw.githubusercontent.com/kwyntes/crossshell/main/linux_or_macos_code.sh|sh;echo >/dev/null \'
Note that the syntax highlighter doesn't actually understand what's going on either.
Short answer: By abusing almost every single syntax feature of each shell.
Long answer: Here's an almost-character-by-character explanation of the whole script:
Character sequence | Bash interpretation | PowerShell interpretation | Command Prompt interpretation |
---|---|---|---|
echo |
Echo command | ||
^" |
Echo ^ and begin doubleq string literal |
Echo " (^ is the escape character) |
|
>NUL |
<Inside doubleq string literal> | Redirect output to NUL (discard) |
|
\" |
" | Echo \ and end doubleq literal |
Echo \ and begin string literal3 |
>$null |
" | Redirect output to $null (discard) |
<Inside string literal> |
" |
End doubleq string literal | Begin doubleq string literal | End string literal |
' |
Begin singleq string literal | <Inside doubleq string literal> | Echo ' |
& |
<Inside singleq string literal> | " | Command seperator |
powershell |
" | " | Execute powershell ... |
" |
" | End doubleq string literal | Begin string literal |
; |
" | Command seperator | Parsed by PowerShell |
iex (iwr URL) |
" | Download script and execute it | " |
; |
" | Command seperator | " |
echo >$null |
" | Echo and redirect to $null |
" |
' |
End singleq string literal | Begin singleq string literal | " |
>/dev/null |
Redirect output to /dev/null (discard) |
<Inside singleq string literal> | " |
; |
Command seperator | " | " |
curl -s URL|sh |
Download script and execute it | " | " |
; |
Command seperator | " | " |
echo >/dev/null |
Echo and redirect to /dev/null |
" | " |
\' |
Echo ' |
End singleq string literal | " |