82

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!

8
  • 3
    Have you considered looking at Perl? It has file globs, succinct existence checks (print 'exists' if (-e $filename);) and backtick execution.
    – tjdett
    Apr 20, 2012 at 2:16
  • I haven't actually ever tried Perl. It came to mind as I was writing this question, but I've always assumed Perl was also full of incomprehensible syntax. =D I'll check it out — thanks! Apr 20, 2012 at 2:21
  • 2
    It can be. Fortunately you aren't required to actually use most of it yourself. Apr 20, 2012 at 2:44
  • 1
    Say what you will about Perl, but it's a fine alternative to bash once your script exceeds about 15 lines. It's nearly guaranteed to be installed everywhere Bash is. Apr 20, 2012 at 2:58
  • Newer shells allow alot of leaway for backwards compatibility to older shells. You can simplfy your syntax be deciding to always use [[ ..... && .... || ... ]], as an exaggerated, but sometime appropriate example. This style [ .. ] && [ .... ] goes back to the original bourne shell and some people feel compelled to use it for the portibility. Also note you can do any math inside of (( a++ )) or cnt=$(( a++ )) when you need cmd-substituion, admitly a bad example. Can't help with single ';' except to put each command on a separate line. ';;' are specific to case statements. Good luck.
    – shellter
    Apr 20, 2012 at 3:56

8 Answers 8

23

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

1
  • As far as I can tell Batsh project has been abandoned. Using opam version 1.2.2 I could not get this to install (compiler error)
    – benathon
    Nov 10, 2017 at 10:37
15

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!

2
14

I tried all of the above (results) and started powscript.

Differences powscript vs the tools above

  • extremely portable preprocessor (100% bash)
  • balances between coffeescript and bash
  • hasslefree portable all-in-one-file compiler/runtime, written in bash
  • loose transpiler: inline bash always possible
3
  • I'm actually using powscript and really like it. I would just say that the interpreter should be more verbose. Jul 27, 2016 at 22:41
  • Awesome you did this AND tried the others, and thanks for publishing it
    – TonyH
    Apr 27, 2021 at 16:18
  • Can't get this to work. Alas: looks realy good! Dec 16, 2022 at 18:50
8

Bish is another option:

https://github.com/tdenniston/bish

Shell scripting with a modern feel.

Bish is a lightweight language created to bring shell scripting into the 21st century. It gives programmers the comfort of modern syntax but compiles to Bash, resulting in good portability (in as much as Bash is portable).

6

The problem is that the whole strings-based semantics of Bash is so horribly broken, it'd be pretty difficult to do something like CoffeeScript for Bash.

Since you probably don't need function-level interoperability to call functions that are written in Bash, you're better off using something entirely different. Perl is close to Bash in being nasty and full of shortcuts and weird syntax, but its semantics are mostly sound. Python is less comfortable for things such as launching processes but is far better for general systems programming, clean and easy to maintain. Python has great libraries and modules for everything; Perl even better.

1
  • 4
    I really don't think Perl is what this guy is looking for, considering he wants to have less "noise" in his scripts. Sep 22, 2012 at 13:05
5

I recently developed a language called BashClass which is Object Oriented, has type checking and allow multi-dimensional arrays. The language syntax is inspired by different programming languages.

Here's an example on how a List class is implemented (Full example here):

class List extends Object {
    var Object[] data = new Object[];
    var int size = 0;
    constructor List(){
        super_constructor();
    }

    function void add(var Object object) {
        data[size] = object;
        size = size + 1;
    }

    function void pop() {
        if(size == 0) {
            exception("Cannot remove element from an empty list");
        }
        size = size - 1;
        data[size] = null;
    }

    function int size() {
        return size;
    }

    function Object get(var int index) {
        if(index < 0 || index >= size) {
            exception("Cannot access element out of bound");
        }
        return data[index];
    }
}

Classes and multi-dimensional arrays in BashClass are converted to Bash 4.4 associative arrays. The language is at its first release and is open source on Github. Feel free to contirbute and suggest features.

2
  • What am I missing here: Bash has no garbage collection (and also no other meaningful way to manage the allocated memory) and you are also invoking processes that might capture references to whatever you are using there as "object addresses". "Objects" are a design pattern that is supposed to simplify the preservation of a consistent state in small patches of dynamically allocated memory. Since the underlying runtime (bash) offers no meaningful ways to manage the dynamically allocated memory, building fancy higher-order constructs on top of that seems pointless. Sep 7 at 10:52
  • That being said: it's pretty awesome regardless. Sep 7 at 11:02
3

You might want to give ZSh a try, it has a lot of improvements to make your shell script more readable.

http://www.zsh.org

2

You might want to take a look into nscript, in which you can write shell scripts using javascript. All the common bash constructions are in there, like exit codes, pipes, stream redirects, argument expansion, globbing, prompt etc.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.