Skip to content

Cross(s)hell - single shell snippet that works in Bash, PowerShell and Command Prompt

License

Notifications You must be signed in to change notification settings

kwyntes/crossshell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cross(s)hell

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 \'

In theory, the script

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)

In practice, it works!

sh

sh

PowerShell

sh

Command Prompt

sh


The script

With Bash syntax highlighting

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 \'

With PowerShell syntax highlighting

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 \'

With Batch syntax highlighting

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.


How does it work?

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 "
3. Command Prompt doesn't seem to behave consistently with string literals so I'm honestly not exactly sure why this works. It also does not feature single-quoted string literals.

About

Cross(s)hell - single shell snippet that works in Bash, PowerShell and Command Prompt

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published