6

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

 7 months ago
source link: https://dev.to/jmfayard/bash-is-a-terrible-programming-language-but-whats-the-alternative--oc2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Cover image for Bash is a terrible programming language, but what's the alternative ?
sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg 7 multi-unicorn-b44d6f8c23cdd00964192bedc38af3e82463978aa611b4365bd33a0f1f4f3e97.svg 2 exploding-head-daceb38d627e6ae9b730f36a1e390fca556a4289d5a41abb2c35068ad3e2c4b5.svg 2 raised-hands-74b2099fd66a39f2d7eed9305ee0f4553df0eb7b4f11b01b6b1b499973048fe5.svg 2 fire-f60e7a582391810302117f987b22a8ef04a2fe0df7e3258a5f49332df1cec71e.svg 3

Bash is a terrible 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 (25)

pic

CollapseExpand

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.

Comment button Reply

CollapseExpand

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.

Comment button Reply

CollapseExpand

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 ?

Comment button Reply

CollapseExpand

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.

Comment button Reply

CollapseExpand

language = Clojure (with Babashka)

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

Comment button Reply

CollapseExpand

CollapseExpand

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.

Comment button Reply

CollapseExpand

language = Kotlin

Two options :

  1. Lightweights Kotlin scripts are a thing
  2. A full Kotlin project compiled to a binary executable with Kotlin native

I liked the idea of it, but right now Kotlin scripts are very niche and not quite supported.
The native toolchain is quite heavy, the compiler is slow.
Packaging is not really ready.

So right now it's better in theory than in practice, probably use something else.

Comment button Reply

CollapseExpand

I do agree that BASH is hard to learn initially and not easy on the eyes but once you get used to it, it's not that bad. Why not just learn BASH and the BASH way of doing things? Instead of trying to use it as a full-blown programming language, use it rather for administrative tasks like parsing log files.

You shouldn't use BASH for complex scripts, it's meant to just perform sysadmin tasks.

Just imagine if you want to copy a large folder to a remote server:

rsync -av  somediretory  bkuser@backups-server:/backups/

Simple one-line command. Now in a programming language, you have to "glob" and loop through files and then use some kind of module or drop to a shell of some sort.

This is how BASH is meant to be used. Plus it allows for pipping, you can use "xargs" and pipe several files in parallel to be synced to the remote server. This is 2 lines of code VS having to do this in Python or Node would take several lines more, probably 20+.

There are several alternatives like ZSH, FISH, and so on, but ultimately on a Linux/Unix system, BASH has close bindings to the OS kernel itself, thus making some advanced tasks pretty straightforward.

Python and Perl is an alternative if you prefer a full-blown language. These should be able to do most of what you can do in BASH.

Comment button Reply

CollapseExpand

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 / ...

Comment button Reply

CollapseExpand

Absolutely! I understand what you mean and makes sense. I have been thinking of writing a Python interpreter that compiles down to BASH similar to what TypeScript is for JavaScript.

It was too much work though to just scratch my own itch :-) Maybe I might look at that if there is an overwhelming need.

Comment button Reply

CollapseExpand

I kind of agree, in that I think if there's anything you need to code more than a very simple for loop then you would be better off using any available scripting language.

Having said that, I write a lot of shell scripts. I try to make them POSIX if at all possible, and that's kind of my cut-off point for choosing a different language. If you need to use a bash extension for arithmetic then either fall back on bc or make the jump to Python or whatever.

... but there's no point in using Python if the majority of what you're doing could be accomplished by calling grep in a subshell.

Comment button Reply

CollapseExpandCollapseExpand

Am I missing something here or are you referring to something other than the UNIX SHELL?

I have to laugh at you modern-day warriors whining about something that's probably older than most of you. Personally, I've always used C shell but that's just 1 old man's opinion. Until you've debugged memory allocation issues with DBX, please with your outrage! It's petty!

Your answer might be PERL. It's your basic C or PHP syntax and is powerful enough to 86 SED and AWK via a single command. If you're complaining about the limited time when you use BASH with your repo check-ins, please write a COBOL program to calculate the compound interest of a 30-year mortgage. No, please do .. your head will explode!

Comment button Reply

CollapseExpand
Comment marked as low quality/non-constructive by the community. View Code of Conduct

cringe

"hey, look how I'm old and used to old tools, you kids are dumb, don't know what is really hard" 🧓🧓

Comment button Reply

CollapseExpand

I'm disagree with all of you who thinks bash is bad or hard to learn. I am using both, Bash and Python, for over 3 years and maybe more and in some cases I prefer Bash over Python because would easier to write it in Bash and the performance would be slight better than Python. Try comparing Vim or Lua scripting to Bash and you'll realise how easy to learn is Bash.

Comment button Reply

CollapseExpand

Bash is hard to learn, period. If it was easy, there would be few people complaining about it. If it's famous for being hard to learn, it's hard to learn. It's not about opinion.

Comment button Reply

CollapseExpand

Until reading this post, I had never heard anyone say bash was hard to learn. I've been writing software for 18 years.

Comment button Reply

CollapseExpand

You need a language that every decent Linux administrator knows in order to have continuously.
That is bash.
Use perl, python or another language and almost no administrator can read it.
Even the bash scripts shouldn't be too complicated.

Comment button Reply

CollapseExpand

GitHub logo nushell / nushell

A new type of shell

Nushell

A new type of shell.

Table of Contents

Status

This project has reached a minimum-viable-product level of quality. Many people use it as their daily driver, but it may be unstable for some commands. Nu's design is subject to change as it matures.

Learning About Nu

The Nushell book is the primary source of Nushell documentation. You can find a full list of Nu commands in the book, and we have many examples of using Nu in our cookbook.

We're also active on Discord and Twitter; come and chat with us!

Installation

To quickly install Nu:

# Linux and macOS
brew install nushell
# Windows
winget install nushell

To use Nu in GitHub Action, check setup-nu for more detail.

Detailed installation instructions can be found in the installation chapter

Comment button Reply

CollapseExpand

This Oz the same problem JS/Ecma has. There is no valid replacement for the role or fills. Not worth the install base now, now or ever.

Comment button Reply

CollapseExpand

I think the best alternative is to script using yaml files 🤓

Comment button Reply

CollapseExpand

CollapseExpand

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

Code of ConductReport abuse


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK