6 ms·
The stigma is well deserved and I will enthusiastically spread it. Bash should be considered an esoteric programming language. Shell Programming Considered Har
by AceJohnny2 5y ago
The stigma is well deserved and I will enthusiastically spread it.
Bash should be considered an esoteric programming language. Shell Programming Considered Harmful.
It's hard to program, and easy to make mistakes in. It should be left as a tool of last resort.
(that said, I understand the appeal of doing weird things with it, like the Minecraft server, for the same reason other esoteric programming languages have appeal)
- asdff 5y agoWhat is so hard about it? It feels like python but with an even more english language like syntax, and the man files can be like tomes of knowledge. 'head file.txt' is a heck of a lot easier to wrap your own head around than reading something into an object and calling head on it. Python programmers today would probably import pandas for that.
- scubbo 5y agoNot the person you replied to, but - yes, `head file.txt` is perfectly reasonable, as are a few other use-cases like `grep`/`sed`-ing. But when you go beyond "processing streams of lines of text" and start having to introduce conditionals, maps, or functions, the syntax is (for me, at least) opaque and easy to make mistakes in.
- marginalia_nu 5y agoIsn't that also a case against learning new programming languages?
- vore 5y agoI think bash has a lot of gotchas due to its original purpose, and it's definitely more error-prone to write bash scripts than to e.g. write Python scripts, so that should be a consideration. Ideally we should all try to more programming languages that are harder for us to shoot ourselves in the foot with, rather than easier ;-)
- marginalia_nu 5y agoI think considering the purpose of the language, it does rather well. It's really easy to spawn and pipe processes, far more so than in almost any alternatives. Like it's so easy that it's easy to forget that this is like a 5-10 line overhead dance to launch and read from a process in most languages. The problem usually come when you attempt to use it for nontrivial programming. But that just not what it's for, any more than the difficulty in building an OS kernel is a legitimate complaint against PHP.
- vore 5y agoI think this is what the post you were replying to is saying.
- chasil 5y agoStephen Borne famously disliked C, and much preferred Algol. This is important to know in the context of what you are saying. The Algol structures in the Borne shell are the most incongruous parts of UNIX. https://research.swtch.com/shmacro https://research.swtch.com/shmacro
- chasil 5y agoI will just leave this here. "I am a strong believer that Bourne-derived languages are extremely bad, on the same order of badness as Perl, for programming, and consider programming sh for any purpose other than as a super-portable, lowest-common-denominator platform for build or bootstrap scripts and the like, as an extremely misguided endeavor. As such you won’t see me spending many words on extensions particular to ksh, Bash, or whatever other shells may be popular." http://www.etalabs.net/sh_tricks.html http://www.etalabs.net/sh_tricks.html
- marginalia_nu 5y agoI think it's easy to sort of gloss over how good shell scripts are at the sort of things they actually are good at. Consider this: for f in *png; do convert $f $(echo $f | sed 's/png$/webp/'); done This is like at least a screen of code in in most languages, you get that sinking feeling where you need to fork a process and deal with its lifecycle and deal with some standard library to list files and don't forget to close the file handle for the directory and god forgive if you run a program with actual output then you need to read from a pipe. Like, geesh, do I have to write a whole program to convert a few files? In a shell, it's just something you type up because you needed to convert a few files. Don't get me wrong, shell scripts are extremely bad at most things involving non-trivial branches, long-lived state, really most things other languages are good at, but man are they ever good at some things most programming languages are bad at.
- pyuser583 5y agoI can tell you’ve dealt with lots of Python programmers.
- _dain_ 5y ago>What is so hard about it? quick: without looking anything up, write something in bash that takes a string of comma-separated values and turns it into an array of those values. then check to see if your answer is on this very long list of wrong implementations: https://stackoverflow.com/a/45201229/6107981 https://stackoverflow.com/a/45201229/6107981
- asdff 5y agoEasy, I just avoid these sort of mind game situations when programming, and I come up with a simple 'wrong' approach that will probably work fine for my purposes anyhow. I don't care if my implementation breaks if I have a newline in my data or whatever, because I already know based on what I am working on that I won't have to worry about a newline in my data or any other weird quirks they bring up in stack overflow comment sections that I have yet to come across in my years of writing code that gets the job done. I don't care if my solution is fractions of a second slower, it's probably already going to be mindbendingly fast compared to the code running on hardware from 40 years ago when people had to be a lot more mindful about selecting the most performant solutions when writing in bash or anything else.
- _dain_ 5y agoOkay, but how do you know that those weird quirks won't be present in your data? Or what those weird quirks even are? The details of the stuff in that Stackoverflow comment have already fallen out of my head; I couldn't tell you about them now. With Bash there are so many traps and footguns and (subjective) unknown unknowns, it would keep me up at night worrying about it. There's always another maddening fact to discover. I would just use Python, where strings are sane and lists are sane: my_string.split(", "), boom, done, no jumpscares.
- rascul 5y ago> Bash should be considered an esoteric programming language. Shell Programming Considered Harmful. I disagree. Bash is fine. Nothing esoteric about it. > It's hard to program, and easy to make mistakes in. It should be left as a tool of last resort. It's easy to program, but it has a learning curve. Use shellcheck. It's OK to go first to bash if you need a shell script. Other languages make pretty crappy shell scripts.
- pizza234 5y agoI copy/paste from a previous post. "Exoteric" is a stretch (in particular, because there are no alternative glue languages, if we exclude all the shell variants), but cryptic, unintuitive and error-prone fits well. Arrays: - `${#myvar[@]}`: unreadable - `mapfile myvar < <(grep pattern file)`: "mapfile" is not exactly a clear term (there's the synonym "readarray", but "mapfile" is the reference; worse, you may find both used inconsistently); the command as I wrote it also has two problems Strings: - `${myvar##.*}`: unreadable - `echo $(IFS=,; echo "${myvar[*]}")`: unreadable; most also don't know the difference between `[@]` and `[*]`; this doesn't also work for two-char join operations - `[[ $myvar =~ $(echo '\bpattern\b') ]]`: unreadable, and most don't know why command substitution is needed Redirections: - `3>&2 2>&1 1>&3`: most don't know what this means
- marklgr 5y agoYes, it can be cryptic and not beginner-friendly, but it's powerful and once you know it, you know it; it will be useful for years if not decades, as opposed to some tools or frameworks du jour that are sometimes as cryptic. The main issue is that many people try (by necessity or not) to cobble scripts together without really learning the language and its idioms. It's not that hard to really learn it though, it's basically one man page then the mandatory [wooledge wiki][1]. [1]: http://mywiki.wooledge.org/BashGuide http://mywiki.wooledge.org/BashGuide
- rascul 5y agoI don't understand, none of that is unreadable or weird to me. Are you sure the issue is not a lack of familiarity with the language? The language can appear deceptively simple in the beginning.
- kdtsh 5y agoAnd what a tool of last resort it is. It may be much easier (and most of the time I would generally prefer) to use Python for scripting purposes, but Bash is a) almost always available and b) widely portable. If you manage any servers where you never know if Python is available or if it’s version 2 or 3 (and you have no power to change this for one reason or other), you know the value of this. No, it’s not ideal, but we live in a non-ideal world. Long live Bash.
- AtlasBarfed 5y agoBash has the highest rate of Stackoverflow searches per line of code produced. I swear I do about 10 searches per line of code produced. And when you consider there really isn't a standard library, mostly gee-I-hope-this-UNIX-util-is-installed...
- francis-io 5y agoI think the big thing going for it is it's low footprint and mostly universal existence on all servers I work on. For example, I'm working on a Hashicorp Vault server install and due to it's sensitive nature, I'm strongly considering using bash to set it up over something like python. I love the ability to test in a robust language, but the extra dependencies are hard to justify, at least on a security sensitive application.
- einpoklum 5y ago> hard to program Well, we "write" one-liner programs with it all day every day - since it's the shell. I'm not saying bash doesn't have a bunch of warts, but - what other shell language would you suggest as an alternative to it?