Create Linux Bash and Windows batch scripts simultaneously with Batsh
Mixed Doubles
Batsh kills two birds with one stone: Programs written in this language can be compiled both as Linux Bash scripts and Windows batch files.
Linux users like to use Bash scripts to automate repetitive tasks. In the Windows world, batch (.bat
) files are used in the same way. However, the commands used in Linux and Windows scripts are often significantly different. Those who work in both Linux and Windows environments therefore not only have to create and maintain two separate scripts, they also have to grapple with various cryptic instructions.
However, this situation should end soon. Google employee Carbo Kuo, a Chinese native living in Switzerland, is currently working on a programming language called Batsh, whose code is translated by a compiler into the appropriate Bash or batch script.
Welcome Ritual
The following line in Batsh will suffice to output the popular message Hello world!:
println("Hello world!");
You can see for yourself what the compiler makes of this on the Batsh compiler website [1]: Type the line in the text box on the left and then click on Compile to Bash. The web page then compiles the Batsh programming code and displays an equivalent Bash script on the right-hand side. In this case, you see the line:
"echo" "-e" "Hello world!"
To generate a batch file for Windows, click on Compile to Windows Batch. Figure 1 shows the results for the Hello World example. The text box on the left-hand side supports various functions when inputting programming code, such as color highlighting and automatically closing parentheses.
If you want to work with Batsh, you should have at least some experience writing small Bash scripts or programming in another language, because the documentation only consists of a very short and still incomplete reference [2].
Fast Learner
Batsh is based to some extent on the C programming language, but it gives you the freedoms of a scripting language. Listing 1 shows a simple sample script that pieces together the file name, photo1.tiff
, from individual parts.
Listing 1
Batsh Example
First, the variable number
stores the number 1
. As in C, each instruction ends with a semicolon. The second line then stores the text image
in the variable prefix
. Character strings must always be in double quotes; single quotes are not allowed in Batsh. The ++
operator in the third line concatenates two strings. The file name photo1.tiff
is thus stored in the variable filename
. Finally, the last line outputs the complete file name. As in C or Java, you can initiate comments with two forward slashes. The complied Bash script is shown in Figure 2.
Frequently used command sequences can be packaged in a function. Figure 3 shows a simple example. The notation should be familiar to programmers of Bash scripts: The name of the function follows the function
keyword. All instructions to be executed by the function follow in curly brackets. In Figure 3, function filename
only outputs one piece of text. Finally, the last line calls up the function and thus the instructions contained in it.
In the Front, Out the Back
Parameters can be passed into the function using parentheses. Listing 2 illustrates how this works: When you call the filename
function, the number 1
ends up in the num
variable, and the end
variable holds .png
. These two variables are then available in the function.
Listing 2
Passing Parameters
The filename
function then assembles a file name; however, this time it does not write this to the screen with println()
; instead, it returns the name to the calling function, which receives the filename in the dn
variable and displays the return value in the last line. For more on displaying output, see the "Printed" box.
Printed
The Batsh println()
function has a sibling named print()
that also displays the text you pass in at the command line; however, it does not use a newline between the elements. Both functions support any number of parameters. The following example outputs the text Hello World:
a="World"; print("Hello ", a);
An alternative to print()
is the undocumented echo()
. The Batsh compiler translates print()
to "echo" "-ne"
; however, it translates the echo()
function to just "echo"
.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.