top of page
book of spells.webp

Start your journey towards mastering the CLI with our FREE Command Line Book of Spells.

By entering your email address you agree to receive emails from Command Line Wizardry. We'll respect your privacy and you can unsubscribe at any time.

  • Writer's picturePaul Troncone

Creating Hybrid Scripts - Leveraging the Power of Multiple Shells


You may think of command line scripts as using one specific shell or language, but depending on your environment you can combine multiple shells in a single script to leverage the advantages of each.






Hybrid bash script in Windows


If you are running Windows and have Git for Windows installed you can use the included bash shell to execute a script that integrates bash, Windows commands, and PowerShell. Here is an example:

printf 'This is bash running on Windows\n'

powershell write-host "This is PowerShell"

tasklist   	         #This is a Windows command

tasklist | cut -c1-25	 #Windows command piped into a Linux command

windows_hybrid.sh


Here is sample output when run from Git bash:

$ ./hybrid.sh

This is bash running on Windows
This is PowerShell

Image Name                     PID Session Name        Session#
========================= ======== ================ =========== 
System Idle Process              0 Services                   0
.
.
.

Hybrid batch script in Windows


This same method can be used when writing a Windows batch script. The Windows Subsystem for Linux (WSL) and PowerShell can easily be accessed and integrated if installed:

echo This is a Windows command

powershell write-host "This is PowerShell"

wsl cowsay 'This is a Linux command using the Windows Subsystem for Linux'

hybrid.bat


Here is the output when executed from the Windows command prompt:

c:\>hybrid.bat

This is a Windows command
This is PowerShell
 ___________________________________
/ This is a Linux command using the \
\ Windows Subsystem for Linux       /
 -----------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Hybrid PowerShell script in Windows


From a Windows PowerShell script you can easily access Windows commands and Linux commands if WSL is installed:

write-host "This is PowerShell"
wsl cowsay This is Linux

echo "This is a Windows command"

hybrid.ps1


And the output when run from PowerShell:

PS C:\> .\Hybrid.ps1

This is PowerShell 

_______________
< This is Linux >
 ---------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

This is a Windows command

Hybrid bash script in Linux


Finally, you can call PowerShell from a bash script in Linux if you have it installed:

printf 'This is a Linux command\n'

pwsh -command write-host "This is PowerShell on Linux"

linux_hybrid.sh


And the output when executed from Linux:

$ ./linux_hybrid.sh

This is a Linux command
This is PowerShell on Linux

Performance


Creating hybrid scripts is a great way to leverage the strengths of different shells. However, be aware that performance may be an issue. Every time you call a shell that is different than the one the script is natively executing in, it will create a new instance. For example, if you are executing a Windows batch script and call a Linux command using WSL it will create a temporary Linux shell to execute the command. Constantly creating and destroying shells from within a script will negatively impact performance, so avoid using this method in loops or where speed is essential.


Summary


The table below summarizes what shells your scripts have access to from a given execution environment. For example, you can access Linux, Windows, and PowerShell commands when executing a bash script on Windows using Git bash.


Links and Resources


839 views1 comment

1 Comment


robert
Aug 03

This is pretty wild! I've been using all three of these consoles for years, and never knew they could be combined. Thank you for illuminating this!

Like
bottom of page