r/ProgrammingLanguages • u/myringotomy • Sep 22 '18
Does anybody know of a language that compiles to bash?
Bash syntax is awkward and hard to read. It's also not strongly typed. It would be quite useful for have a language which compiles to bash (or sh for that matter) but is strongly typed. Does anybody know of one.
21
Upvotes
18
u/theindigamer Sep 22 '18
What makes bash a good compilation target in your opinion? The only reason I can think of is that most Linux systems already have it.
If you want a type system, why not use a DSL in your typed language of choice?
5
u/calligraphic-io Sep 23 '18 edited Sep 23 '18
Use Perl. It used to be that shell scripting languages were intentionally very minimal (no arrays in C- or Bourne-shell, for example), because whenever a script hit four or five lines you'd move it to Perl. But then BASH got popular with Linux, and it's just "featured enough" that people quit using Perl and instead started writing large systems in it (which is crazy).
Perl is great for system scripting. It has good text-handling and a nice syntax for executing system commands. But Perl lost its path a bit also, adding OOP support, etc. The net effect has been (1) no one uses Perl anymore; (2) people use BASH for all kinds of things they shouldn't; (3) Python is the "new" new systems scripting language popular with admins.
Python is installed by default on RHEL, Fedora, CentOS, Debian, Ubuntu, and OpenIndiana (Solaris), as is (I believe) Perl. So both are reasonable cross-platform targets. Perl is much nicer than Python for handling text.
9
u/nullmove Sep 23 '18
To answer the actual question, there is Batsh (OCaml), Bish (C++) and Shell-Monad (embedded DSL inside Haskell, nevertheless can generate POSIX compliant shell script).
But anyway, regarding your complaint against DSLs in typed languages, wouldn't statically linked binaries solve the dependency issue? If so then you can use e.g. turtle.
10
u/[deleted] Sep 22 '18
There was an Idris Bash backend.
8
u/typish Sep 23 '18
It's not really what you are looking for, but given the emphasis on configuration using a sane language, take a look at Nix and NixOS :)
1
u/nyisdead Nov 04 '21
Question's like this haunt my nightmares, causing me to wake up, screaming about lambs and lotion. I realize that this is old, but I just, can't, help, myself and if I manage insult one moron, then its worth it.
> Bash syntax is awkward and hard to read.
Do you ever stop to consider that something you don't like, or doesn't appeal to you, exists for a reason that you fail to appreciate at the time? Shell has been the standard bearer, for literally forever and time tends to be arbiter of everything - if it wasn't well suited to that environment, it wouldn't exist.
> It would be quite useful for have a language
Useful to whom? That's rhetoriocal, but you do understand, within the context of job, that someone else is going to touch that dumpster fire. If its bad enough, they may, have to hunt you, to the end of the earth.
> which compiles to bash
I dont think anyone understands what compile actually means anymore; what you mean is transpile and you want to write solutions, in something awful like node, and have it transpile to shell, against a platform that is as sensitive as it is powerful.
> is strongly typed
sorry, I was wrong, not node by typescript, right? So you want to replace a purpose-built, concise and terse language with an extremely expressive, and verbose one, and you want to make sure that type concerns explicitly managed, all of which will increase dev overhead on an order of magnitude, with the end goal being to transpile it back to a terse, and concise shell script in the first place?! I just threw up in my mouth a little..
Dude, to you and all of the other bootcampers: shell looks informal and awkward to you, because its entire purpose is to act as glue between isolated, concurrent and often parallel processes. Shell is slow, because its not meant to be fast: its meant to provide a mechanism to transfer data between fast af processes, using their respective STDIN/STDOUT.
You can use typescript, or node or php or whatever **alongside** shell, as opposed to replace it wholesale. If you had three nodejs scripts, to mimic the concept of a factory line, or here, the steps needed to make cake ready for store shelves, it would be expressed like this.
```sh
ingredients.js | bake.js | package.js
```
The shell is minimal, while all of the complex, stomach turning work is done in the higher level language.
Ok, sorry, end of rant
2
u/PegasusAndAcorn Cone language & 3D web Sep 23 '18
I believe /u/oilshell is working on something in this area, and may have some valuable perspectives to share.
1
u/cooyeah Sep 23 '18
This seems like an interesting idea. I can think of a use case when I want to share my script with others but i don't want to ask people to install a new language interpreter/compiler, or ask them to trust my built binary. Of course that requires the compiled bash to be still kind of readable.
1
u/FCOSmokeMachine Sep 23 '18
https://github.com/spitsh/spitsh
0
u/[deleted] Sep 22 '18
[deleted] — view deleted comment