Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any languages that compile to Bash?

I both love and hate writing Bash. I love that it's so streamlined for operating on files and working with processes (I agree with this popular question that it's way better in this regard than Python, Ruby, etc.), but I hate the syntax, particularly around conditionals, loops, etc.

(This is subjective, but I find it both confusing and annoying. E.g. $var when reading, but var when writing; writes silently fail if there are spaces around =; the double brackets in ifs when using regexp; double semicolons sometimes and single semicolons others; etc.)

As a huge fan of CoffeeScript, which compiles to JS, I've been wondering: are there any languages that have the aesthetic/syntax of languages like Python/Ruby/CoffeeScript but which compile and run as Bash instead of one of those other runtimes?

E.g. I'd love to be able to write mostly-Bash with just a bit simpler syntax:

$AGGREGATE_FILENAME = 'allfiles.txt'  if not exists $AGGREGATE_FILENAME     touch $AGGREGATE_FILENAME  for $file in files/*     cat $file >> $AGGREGATE_FILENAME  switch $1     case 'test'         run-tests         echo 'Tests finished!'     case 'deploy'         echo 'Packaging...'         mv foo bar/         deploy-bar 

This is a super contrived example, and the syntax is a strawman (mostly inspired from CoffeeScript but keeping the essential Bash notions of first-class commands, separated from variables, and loose typing).

Anyway, just a question and food for thought. I'd love to be able to write my scripts in something nicer than Bash. =) Thanks!

like image 940
Aseem Kishore Avatar asked Apr 20 '12 01:04

Aseem Kishore


People also ask

What language uses Bash?

Bash is a powerful programming language, one perfectly designed for use on the command line and in shell scripts. This three-part series explores using Bash as a command-line interface (CLI) programming language.

Is Bash a compiled language?

It means C is a compiled language and Bash is an interpreted language.

Is Bash built on C?

Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It is intended to conform to the IEEE POSIX P1003.

What language is Bash similar to?

Besides being the superset to the Bourne shell syntax, Bash also includes features from other shell scripting languages such as KornShell (ksh) and C shell (csh) — for example, command-line editing and command history.


2 Answers

You could also try Batsh, which is a DSL (Domain-Specific Language) that compiles a C-syntax language to Bash (and Windows Batch).

  • Project
  • Online demo
like image 86
BYVoid Avatar answered Oct 09 '22 04:10

BYVoid


Since I originally asked this question, two projects have been released which attack this problem and do a pretty good job. Both reimplement many/most Unix tools in more programming-friendly runtimes.

Plumbum is implemented in Python and looks pretty solid:

http://plumbum.readthedocs.org/en/latest/index.html

ShellJS is implemented on Node.js and also looks pretty good:

https://github.com/arturadib/shelljs

Exciting developments! I'm looking forward to trying them out. If you already have, it'd be great to hear your experiences in the comments. Thanks!

like image 43
Aseem Kishore Avatar answered Oct 09 '22 05:10

Aseem Kishore