
A language that compiles to Bash and Windows Batch - onecooldev24
https://github.com/BYVoid/Batsh
======
thinkpad20
The idea is interesting, but ultimately the utility of this seems limited. The
differences between windows and Unix are more than just the shell language
involved; shell scripts are typically deeply intertwined with the system in
question, to the point where it's often not the case that a bash script will
even run reliably across different Unix systems, much less on windows. Also,
you can already run bash on windows, so once again the problem doesn't seem to
be the language per se. I can only imagine how difficult it would be not just
to design a script that would work properly on both platforms, but to debug
the cases where it didn't.

Also, as others have noted this language doesn't support redirection, which in
my mind makes it practically useless beyond toy applications. I've written
hundreds of bash scripts and I don't think any of them didn't make heavy use
of pipes and redirection. I'm also not sure if the language supports running
processes in the background, traps, magic variables like $!, extra options,
subshells, file globbing, etc, all things that many real-world scripts use.
Bash scripts often use many programs available on Unix systems as well, such
as find, grep, readlink, xargs, and other things that aren't part of the
language _per se_. Unless those are ported over too, writing useful scripts
would be almost impossible.

Finally, I don't think the author has made a convincing argument that such a
language is even needed, when languages like perl/python/ruby exist for when
the task is complex enough to require a more powerful language. On the other
hand, if the project is (as I suspect) purely for fun and interest, then by
all means :)

~~~
marshray
The attraction for me is not about portability, it's about having a better
language to write CMD script.

Even when you know you have Python available sometimes you still need to glue
things together with CMD.

~~~
jheriko
> sometimes you still need to glue things together with CMD

why? is there something cmd can do that python can't? one example is enough...

~~~
robertelder
I don't use windows much anymore so there might be a better way to do this,
but I'll propose a use case: Automating the installation of python on a fresh
machine.

~~~
xyience
Fresh machine where and for whom? I doubt a simple batch script will get you
very far (setup.exe exists for a reason...), and I doubly-doubt you would have
enough overlap in the process that something that can convert the steps to
bash would be an advantage. The only advantage I see for batsh is that it
looks nicer to write in for the tiny subset of batch it supports. But then so
is pretty much anything else -- it wouldn't surprise me if someone has already
written a DSL in a well known language that can output to batch or powershell,
we don't need a whole new language for this.

~~~
robertelder
Ok, I'll try another use case: Political differences (specifically licensing).
This project appears to be licensed under MIT, and python appears to be
licensed under !MIT. I don't know what the implications are for the licensing
of machine-produced shell code, or binary versions of Python, but I'm sure
there are companies out there that would have opinions to favour one
alternative over the other for reasons other than the technical merits of the
choice.

~~~
xyience
Maybe. Python is under an MIT-like license, so you're fine, but if it were
under e.g. the GPL you could still look to see if they have some form of
runtime exception as GCC does. In any case it's rare for a company that's not
well-established to care more about licensing that technical merit (and even
any that do, the typical approach is to ask to buy a separate license if
possible), and it's also rare that a company would use such a tool as this
(created as a hackday project, in Ocaml, according to the author) rather than
finding an alternative that meets their licensing criteria or just developing
it in-house since it's not that big a tool and lacks a lot of useful
capabilities like piping.

------
scandox
You've got to admire this and at the same time you can't help but see the
shadow of two more letters on the end of its name...

~~~
angersock
Adopted by ASF as Apache Guano(tm).

~~~
ArkyBeagle
The colonel interface is called 'bat' Guano, but then you have to answer to
the Coca-Cola company.

------
BYVoid
I am the author of Batsh. Batsh is a toy language I developed 2.5 years ago in
a hackathon. It was just for me to play with OCaml. Feel free to play with it
at [http://batsh.org/](http://batsh.org/).

~~~
xyience
This deserves to be called out in the readme, if for nothing else than to
limit the wild threads on display here and maybe head off some future wild
threads.

------
jtwebman
The project hasn't been worked on in over a year also it isn't the language
that is much different but the tools. Are you going to write grep on windows?
If not the language really doesn't matter.

~~~
alkonaut
If the language has a string search command then it can implement its own, or
it can delegate to grep on Linux and Findstr on Windows. Obviously in both
cases it can't cover all that grep can do, but that likely isn't in scope. All
I'd want is a tool that lets me maintain _one_ build script for example.

------
legacy2013
This is cool, but I hope the new Linux Subsystem in Windows 10 will propagate
enough that everything can be written in bash

~~~
tk32
i will never understand why people fetishize that awful language.

~~~
dahart
I think it's your incorrect assumption that bash's popularity is due to it
being a "fetish" for anyone. There aren't alternatives that are both better
and more available / cross-platform / standard.

I agree its a crappy language, but remember, it's a shell, not a language. It
serves a different purpose than being good for pure code, so saying something
like "<your favorite scripting language here> is better than bash" doesn't
hold a whole lot of water. It is way better than batch though.

~~~
AnimalMuppet
"<your favorite scripting language here> is better than bash _at the kind of
tasks that bash is used for_ " would be a pretty substantial claim, though.
(Unfortunately, it's almost never the claim that people are making. "X is
better than bash at creating UIs/database lookups/OO/web programming"? Yeah,
of course it is. Water's wet, too.)

------
xrstf
Is there a way to get this as a precompiled Windows binary? I really want to
give this a try, but I'm just sooo lazy and don't want to install OPAM if
there's maybe a way around that.

~~~
zem
ironically, the instructions there don't tell you how to get opam installed on
windows either. i don't know the answer, but apparently [it's not
trivial]([https://github.com/ocaml/opam/issues/246](https://github.com/ocaml/opam/issues/246))

------
pm
Was hoping the website was a .it domain.

~~~
OJFord
We'll just have to wait for people to ask "how do I make this shell script
platform independent?" (batsh it)

------
onedognight
I was disappointed to see that the compiler was not written in batsh and
therefore not self hosting. Seems like a missed opportunity.

------
Someone
Weird that it doesn't seem to support _basename (1)_ and _dirname (1)_ and
their Windows equivalents (e.g. _%~dp0%_ )

Quite a few of my scripts need to find files relative to the location of the
script, or compute the path to an output file by replacing the extension of an
input file.

~~~
RGamma
As I read it, it is mainly a language that intends to "unify" bash and batch,
but basename(1) is not so much a part of bash, but of POSIX, that is an
executable in a POSIX environment, that can be called via bash.

However it does say "Batsh provided some "built-in" functions that will
compile to platform-dependent code.". Arguably you could count basename and
dirname under that.

~~~
kevin_thibedeau
The builtins are currently too limited. It looks like to get anything done you
have to shell out to commands with call(), bash(), and batch() which leaves
you with nice looking control structures surrounding a whole bunch of
conditional code tailored for each platform. Path manipulation is an important
missing feature. Some sort of array handling and filtering/sorting also needs
to be abstracted.

------
tomcam
I must have done it wrong. In the late 80s, I created a Windows batch-to-
executable compiler called Builder (extending the language greatly, to the
point of menus as language constructs). Made a decent living off it, too!

~~~
bbcbasic
Consider posting in here then:
[https://news.ycombinator.com/item?id=11718197](https://news.ycombinator.com/item?id=11718197)

------
nikolay
I posted Batsh long ago, but it's totally impractical. The multiplatform
PowerShell is the best option. Anyway, we need a new shell and NGS [0] looks
like it. Neither zsh, nor Fish offer what a shell scripting language needs so
that you don't have to use Python, Go, or another language for a slightly more
complex stuff!

[0]:
[https://news.ycombinator.com/item?id=11734622](https://news.ycombinator.com/item?id=11734622)

------
moondev
Since docker is on windows now, this should run script.sh in current directory
pretty easily.

docker run -v %CD%:/opt -w /opt debian:jessie ./script.sh

------
ricksplat
A couple of slight nitpicks, but isn't this more like a translator? I
understand a "compiler" to be that which condenses high-level structures down
to more fundamental CPU-oriented structures (as opposed to an "interpreter"
that takes the structures as presented). If you're compiling into Bash, or
windows batch then you're converting from one high-level representation into
another. Though you could debate whether these are "high level" whether they
are technically or not. I will concede that this _batsh_ language does indeed
look a lot nicer than either - it seems to be of the "general purpose" style
of C or Javascript rather than the more targeted style of either bash or batch
- which is specifically for the domain of operating system scripting - I guess
this might mean it's actually _lower_ level. The whole thing is pretty cool
though :-)

------
kelvin0
Cool stuff! Would have been useful for me when all I knew was C++!
Unfortunately, I use Python for exactly this type of scripting. Oh and I also
use it for stuff like Django, lxml, requests, wxPython, OpenCV, PyGame, NumPy,
Reportlab ...

------
zwieback
Looks nice but wouldn't you just use something like Python nowadays?

~~~
thoth
The "Why not Python/Ruby/Node.js/Lua" section lists 3 reasons the author
developed this language...

~~~
sigjuice
Bullet 1 says "None of them is preinstalled on all platforms", but OPAM isn't
universally preinstalled either.

~~~
merb
OPAM is only needed on the computer you develop the script. Actually you guys
shouldn't be so lazy to just write something without reading the README.

------
jheriko
how is this top item? seems unfinished, unpolished, not very useful and very
run of the mill as an achievement.

no easily found windows installer and has obscure dependencies whilst claiming
it has none etc. etc.

~~~
sixothree
I don't even see the ability to iterate through files or am I missing
something?

------
juped
Excellent idea. The only thing I thought was "why not PowerShell", but XP
compatibility is probably the reason. (It exists but isn't preinstalled for
that version.)

~~~
badloginagain
Powershell versioning is also a nightmare

------
fpoling
I like the idea of compiling to bash from a much saner language even without
Windows compatibility. With containers and slimmed-down OS like CoreOS Python
and friends may not be available. Besides, for some tasks Python startup time
is just unacceptable so using Bash could be a good option if not the awkward
syntax to put it mildly.

------
exabrial
"Bash is the assembly code of Unix?"

------
marshray
I was yearning for something like this literally just last night. Still am,
tempted to give it a try.

------
incepted
Isn't it ironic to write this tool in a language that is not available on
Windows[1]?

[1] "Windows support is comming soon. "
[https://ocaml.org/docs/install.html](https://ocaml.org/docs/install.html)

~~~
nv-vn
OCaml is available on top of MinGW or Cygwin. The thing that isn't supported
is OPAM (the package manager), and that's only for official support -- if you
compile with MinGW or Cygwin you can get it working well enough for most
tasks. You can actually get the compiler binaries from a precompiled versoin
of the official distribution[1] or from a user-maintained distribution[2].
Additionally, you could just compile everything from source but that's more
complex.

[1] [https://protz.github.io/ocaml-installer/](https://protz.github.io/ocaml-
installer/)

[2] [https://www.typerex.org/ocpwin.html](https://www.typerex.org/ocpwin.html)

------
chris_wot
I love the fact that someone, somewhere needed this, and then someone,
somewhere created it.

~~~
eggy
Me too. He created it at a Hackathon, it gets posted to Hacker News, and
people here are still asking why. Because...

------
eagsalazar2
I've never tried but can't you already just run your node or ruby or whatever
scripts on both Windows, Mac, Linux? Is there some reason targeting bash and
batch is important?

~~~
laported
From the README:

Why not Python/Ruby/Node.js/Lua

Yes you can use any of them as platform-independent glue code. But there are
several disadvantages:

    
    
        None of them is preinstalled on all platforms (including Windows).
        Functionalities like process piping are not convenient to use.
        Hard to integrate with existing code written in Bash or Batch.
    

Those reasons are why I developed Batsh.

------
evincarofautumn
How should I pronounce the name? I read “batsh” as a homophone for “batch”
[bæʧ], which is unfortunate. Is it supposed to be pronounced as two syllables
[bæɂ.ʃ] or something like that?

~~~
bbcbasic
Pronounced "bath". The s is silent.

------
youjiuzhifeng
Cygwin provides another way to run the bash shell on windows. It is really
helpful with full features of some commands like 'find' 'sed' 'awk'.

------
niutech
There is also Pash: PowerShell + Bash
([http://pash.sourceforge.net](http://pash.sourceforge.net))

------
sedatk

      @echo off
      set java=/usr/bin/java
    

that's not how it works. that's not how any of this works.

~~~
_quick_q
Where are you finding that?

------
knocte
At the time GNU/Windows became a thing recently
([https://msdn.microsoft.com/en-
us/commandline/wsl/about](https://msdn.microsoft.com/en-
us/commandline/wsl/about)), the idea of Batsch turned obsolete to me.

And anyway I have been already been using crossplatform .fsx scripts for quite
some time. KTHXBYE

------
emmelaich
This would be a candidate for the scripting language in _redo_

Paging @apenwarr

------
meunier
Opportunity to name this batshit wasted.

------
agjmills
Relevant XKCD: [https://xkcd.com/927/](https://xkcd.com/927/)

