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
What are the worst programming languages that nobody should learn?
Jean-Michel 🕵🏻♂️ Fayard ・ Jan 8 '20
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.
- Just take whatever you have typed in the terminal and put that in a
run.sh
file. - 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.
Top comments (96)
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.
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.
I tried zx, that looks really nice thanks !
no one told me this exists, such a shame 🙏
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 ?
Usually, in addition to the standard integrated Node tools, you would need something like
path
andchild_process
(orexeca
), 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 arelistr
,inquirer
,chalk
, andfiglet
. There's much more to that, even CLI frameworks, but I prefer simpler and more granular tools.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.
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.
I do basically the same. Most of the bash scripts I write just invoke
node
to do the heavy lifting.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 / ...
language
= VThe idea comes from a discussion about V, a modern programming language that on paper seems almost too good to be true.
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
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.
A weekend to get an intro. More tobe good
This is probably assuming you are somewhat familiar with go
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:
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.
Damn! I laughed sooo hard! Reaallly agree on the last point. Argument parsing is hell of a nightmare in Bash.
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 evenbash
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.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
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.
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.
language
= Clojure (with Babashka)It has a really fast start up due to the GraalVM. 😁
This is the answer
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.