Using Bash with PrimalScript
- Written by Alexander Riedel
- Last Updated: 09 October 2019
- Created: 26 May 2016
- Hits: 5858
I have been asked a few times now to use Bash with PrimalScript. It doesn’t have all the setup for it out of the box because there are a few different implementations of Bash available for Windows. If you followed the latest news, Microsoft has announced at the BUILD 2016 keynote that Bash will become an integral part of Windows. (https://www.youtube.com/watch?v=kJGqZHQzNRo)
Since we do not have the bits from Microsoft yet, I used the Cygwin Bash shell to demonstrate the necessary steps for PrimalScript 2016. You can download and install this version from here: https://cygwin.com/install.html
There are a few quirks that require manual intervention after the installation though.
The installer will install the shell in your “C:\cygwin” directory and when you launch bash.exe you get the bash-3.X$ prompt. However, although the pwd or cd commands work, if you try ls, it says:”ls: command not found”.
Here is what you need to do to fix it:
- Right click on “My Computer” -> Properties -> Advanced -> Environment Variables
- Add a new environment variable, called CYGWIN_HOME and set its value to C:\cygwin
- Edit the PATH environment variable and add C:\cygwin\bin to it (usually separated by a ‘;’).
- Just click okay, exit any command prompts or bash shells (over cygwin) you may have open, and open it again – it’ll work!
(Instructions taken from here: http://thehacklist.blogspot.com/2009/04/cygwin-ls-command-not-found.html)
Now that you have Bash running, there are two ways to add this to PrimalScript. Firstly to run bash shell scripts directly and secondly to have a Bash console embedded in PrimalScript.
Adding a new shell to PrimalScript is very easy, since it already supports CMD and PowerShell. So just go to File – Options and select “Command Window”
Please note that after you set this up you will need to restart PrimalScript.
After the restart you need to make sure you have the shell windows made visible if you do not use them normally. On the View tab of the ribbon is a big “Shell” button in the last group on the right.
When you see the console window docked on the bottom, you probably will see one of the default shells, CMD or PowerShell, depending on your OS version and setup. Use the combobox control on the top of the shell windows to switch to your Bash instance.
So there you are, a bash shell inside PrimalScript.
Ok, now we move on to running bash scripts.
The file type template already exists in PrimalScript. When you select File – New, the “Bash” type is in the first row.
Go to File – Options – Language and select “Bash” from the list. Add the settings shown in the image below and press OK.
The Cygwin Bash implementation has, a mentioned before, a few quirks that Windows users may trip over. The line endings of bash scripts need to be linefeed only, as they are in Unix. Using Windows line endings will simple produce syntax errors, even on empty lines. You can change the line endings for a file in the properties as shown here:
(Right click in the file and select “Properties…” to see this dialog)
Last but not least, the Cygwin Bash shell does not understand Unicode. You must use plain old ASCII files. Using Unicode will result in an error message that binary files are not supported. So when you see that error, you know what to do.
Right click on the encoding indicator on PrimalScript’s status bar and change the encoding to “ASCII” if it does not say that already.
Ok, now you are all set and you can use the “Run” button to launch your script. The output will show up in the output window, just like for any other script you run from PrimalScript.