DEV Community

Cover image for Bash is a terrible as a programming language, but what's the alternative ?
Jean-Michel 🕵🏻‍♂️ Fayard
Jean-Michel 🕵🏻‍♂️ Fayard

Posted on • Updated on

Bash is a terrible as a programming language, but what's the alternative ?

I believe in self-compassion so I have a personal rule

Never write a shell script that is more than 5 lines long.

Let's bash Bash

I am on the record to saying that there is no programming language that is never worth learning

Bash is the exception. It's programming stuck in the stone age. No real functions. No argument names. No types. No data structure. No unit tests. No dependencies management. I need to google every time I want to do something basic like a for loop.

I like it when Homebrew or Gradle or whatever allow me to bootstrap their thing with a complex shell script. Because they went through the pain of writing and maintaining their shell script.

But otherwise, my advice is to give up.

But what's the alternative ?

If things are terrible, and they are, what are we stuck with Bash ?

First, because like with every addiction, Bash's appeal is that it's easy to get started.

  1. Just take whatever you have typed in the terminal and put that in a run.sh file.
  2. Upload that to a gist and you have spread the virus.

Second because it's not clear what's a good alternative that has the same initial appeal as Bash without its many pitfalls.

That's where you come in.

What has been your experience with scripting in $favoriteLanguage ?

Top comments (96)

Collapse
 
fyodorio profile image
Fyodor

I used to engaging Node for that. You basically need only Node (which can be installed through nvm at any env via a bare curl call or something). Rich and beautiful NPM ecosystem is at your disposal. Further convenience is the matter of efforts.

But thanks for the V reference, looks interesting.

Collapse
 
cogwizzle profile image
Joe Fehrman

If you like to use JavaScript for shells I think you would like this. github.com/google/zx

I find it easier than using the build in node process utils.

Collapse
 
jmfayard profile image
Jean-Michel 🕵🏻‍♂️ Fayard

I tried zx, that looks really nice thanks !

Collapse
 
zabjd22 profile image
Zetrax

no one told me this exists, such a shame 🙏

Collapse
 
jmfayard profile image
Jean-Michel 🕵🏻‍♂️ Fayard

My issue is that I know the language but not the ecosystem. Browsing npmjs.org is overwhelming.
What kind of npm libraries would you use for the common tasks someone need for a CLI tool ?

Collapse
 
fyodorio profile image
Fyodor

Usually, in addition to the standard integrated Node tools, you would need something like path and child_process (or execa), that could be enough for simple stuff and working with files and trivial scenarios. If you need some nice decoration (for a team, or for pleasing yourself 😅), there are listr, inquirer, chalk, and figlet. There's much more to that, even CLI frameworks, but I prefer simpler and more granular tools.

Collapse
 
ben profile image
Ben Halpern

I find the bigger issue ends up being the sharability.

The problem with any platform is that the further you get from the thing that ships to everyone, the less you can confidently have in common with everyone else.

That's so much of the power of bash, git, etc.

Thread Thread
 
jmfayard profile image
Jean-Michel 🕵🏻‍♂️ Fayard

I strongly agree, distribution is key !

It doesn't have to be bash though, if the thing is easy to bootstrap & index via npm, brew or something, I am all for it.

Collapse
 
mistval profile image
Randall

I do basically the same. Most of the bash scripts I write just invoke node to do the heavy lifting.

Collapse
 
jmfayard profile image
Jean-Michel 🕵🏻‍♂️ Fayard

When I say that bash is bad, I mean shells in general.
They are good for sysadmin tasks, when what I am doing is one program being executed after another.

But I pause and reflect as soon as I find myself using functions / if else / for loops / arguments / ...

Collapse
 
jmfayard profile image
Jean-Michel 🕵🏻‍♂️ Fayard • Edited

language = V

The idea comes from a discussion about V, a modern programming language that on paper seems almost too good to be true.

Image description

My obvious counter-argument is : why would I invest my time learning a langauge that just reached version 0.4 ?

But then I saw this

Cross-platform shell scripts in V
V can be used as an alternative to Bash to write deployment scripts, build scripts, etc. The advantage of using V for this is the simplicity and predictability of the language, and cross-platform support. "V scripts" run on Unix-like systems as well as on Windows.
V is 80% go and can be learned in a week-end.

Ok, fair enough, I would rather spend a week-end learning V than fighting to debug a dumb Bash script, that sounds appealing to me.

Collapse
 
tgkprog profile image
Tushar Kapila

A weekend to get an intro. More tobe good

Collapse
 
jmfayard profile image
Jean-Michel 🕵🏻‍♂️ Fayard

This is probably assuming you are somewhat familiar with go

Collapse
 
autra profile image
Augustin Trancart

Yes for simple scripts, it's enough. Although you are comparing apples to orange, because you can call rsync in one line in python code too ;-) (well, with the import, probably 2 or 3 but well).

My problems with bash:

  • you cannot really unit test it (and yes, I have tested bats)
  • there several ways to do "if", each with their caveats and pitfalls
  • there is no real exception handling
  • there is no real functions (they are commands) and you cannot really return from them (return statement is for error code, anything other than indicating an error is a hack and will bite you)
  • argument parsing is an horror

For these reasons, anything other than trivial is really cumbersome with bash. Especially, as soon as we have arguments to a script, we should just use python and argparse for instance.

Collapse
 
ddebajyati profile image
Debajyati Dey

Damn! I laughed sooo hard! Reaallly agree on the last point. Argument parsing is hell of a nightmare in Bash.

Collapse
 
russellbateman profile image
Russell Bateman • Edited

I have written installation scripts in Bourne (yeah, that's sh) totally 10s of 1000s of lines of code. (Because across many, varied systems where even bash wasn't a common denominator.) Very excruciating, although this was 20 years ago and there was no debugger. Obviously, I created "libraries" and other helps, but I would have preferred Python.

Collapse
 
togakangaroo profile image
George Mauer

Powershell is an excellently designed language which did the hard work formalizing lessons learned from bash's awfulness.

But realistically what I see emerging in the next few years, is not yet another command line scripting language. I would be pretty surprised if non-scripted interaction with the CLI in 5 years still requires people manually typing out well formatted commands piping things between utilities. I already ask chatGPT for half of the things I want to do with the CLI, I'm not sure what's to stop that from becoming much more prevalent.

For scripting in the meantime, python or powershell or another omnipresent alternative will be fine

Collapse
 
hlship profile image
Howard M. Lewis Ship

Babashka (github.com/babashka/babashka) allows me to have my cake (write scripts in Clojure) and eat it too (fast! startup). Having a fully powered language to write even simple things in (because they never stay simple) is near ideal.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

I honestly believe .*sh are an amazing family of domain-specific scripting languages, and this specialisation also makes them utterly terrible outside their domain.

If what you want consists mainly in 1. running executables 2. managing their I/O then all the best tools for that job will likely be languages you wouldn't want to write an HTTP server in, and that's not a problem.

Collapse
 
arcanjoaq profile image
arcanjoaq

language = Clojure (with Babashka)

It has a really fast start up due to the GraalVM. 😁

Collapse
 
eric_stewart_6c37bebed155 profile image
Eric Stewart

This is the answer

Collapse
 
nova38 profile image
Thomas N Atkins

Python is great for some stuff, but to replace lot of the stuff you would use bash for powershell is one of the best scripting languages I have used. Its not perfect but it produces a lot more readable scripts and it is a lot easier to find the utility function you need because of the verb-noun syntax. (Plus it is also cross platform)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.