7 ms·
Bash/Sh is an objectively awful programming language. You could likely fill an entire book with all of the design fails that is Bash script syntax. Only in a sh
by SomeCallMeTim 3y ago
Bash/Sh is an objectively awful programming language. You could likely fill an entire book with all of the design fails that is Bash script syntax. Only in a shell script would you ever need eight backslash characters in a row in order to accomplish something...and "space as delimiter" is awful in so many ways.
But especially Sh runs everywhere, so it's good to know. This seems to be the article's point.
Honestly, though? The right answer is to transpile a better language to Sh.
Looking around, there have been several half-hearted, abandoned attempts. But apparently everyone is happy to just fall back on the "accidental syntax" of Bash that really doesn't make sense when compared to ... any other language.
I put up with Bash because there's no better alternative. That doesn't mean we should put up with Bash; just that we must put up with it at least to the point where it can run code written in a real language.
- danielvaughn 3y agoI'm not very knowledgeable in this area as I don't do much shell scripting, but isn't zsh supposed to be some kind of enhancement over bash? Does it suffer from the same issues? Regardless, the idea of a language that transpiles to bash is kinda interesting.
- SomeCallMeTim 3y agozsh is much better. And it's not installed by default on all systems. It's especially not installed on systems like Alpine. The point was never to "find a better shell." It was to run install (or other) scripts with some level of logic absolutely anywhere.
- NERD_ALERT 3y agoZSH is basically the same thing. For all intents and purposes it is basically the same thing. The best attempt I’ve seen to truly make a better shell is Nushell. https://www.nushell.sh/ https://www.nushell.sh/
- marcosdumay 3y ago> "space as delimiter" is awful in so many ways Yes. But it's also absolutely great in so many ways.
- deleted 3y ago[deleted]
- AtlasBarfed 3y agoUntil you utter the words: Yoo-nee-kode
- sgarland 3y agoShell is an objectively _difficult_ programming language. But difficult != awful. Pointy and ready to bite you if you make a tiny mistake? Yes. Ubiquitous and not going anywhere? Also yes. But given that *nix tooling output defaults to text, it's hard to argue against a universal language that talks to all of it.
- bombcar 3y agoShell works because it’s the ultimate interpreter. You sus out what you need to do by hand and then convert your history into a script.
- zamnos 3y agoTBH I use Python REPL the same way whenever I encounter unknown new libraries, which is why it's my goto after Bash.
- stevebmark 3y agoIn the case of Bash, it's objectively difficult and awful.¹ ¹I have manipulated arrays in Bash, proving this statement.
- sa46 3y agoThere was an effort within Google SRE to use Go instead of bash for all the typical usages of bash. [1] Cloudflare uses Go similarly as a scripting language. [2] [1]: https://go.dev/solutions/google/sitereliability https://go.dev/solutions/google/sitereliability [2]: https://blog.cloudflare.com/using-go-as-a-scripting-language-in-linux/ https://blog.cloudflare.com/using-go-as-a-scripting-language...
- emmelaich 3y agoAlso javascript as an alternative for bash.. Not an official Google product of course but ... https://github.com/google/zx https://github.com/google/zx >Bash is great, but when it comes to writing more complex scripts, many people prefer a more convenient programming language. JavaScript is a perfect choice, but the Node.js standard library requires additional hassle before using. The zx package provides useful wrappers around child_process, escapes arguments and gives sensible defaults.
- JdeBP 3y ago"JavaScript is a perfect choice"? My goodness!
- t-3 3y agoShell is an interactive, concatenative string language with transparent file system access and binary execution capabilities. It doesn't need to be a general purpose programming language, because it's great at what it does. When I need something lower level, I can write that in low level and integrate into the shell language seamlessly.
- norir 3y ago> Honestly, though? The right answer is to transpile a better language to Sh. I like this, but I think it's even better to just escape sh entirely. If you are on a system that supports sh, it almost certainly has a c99 compatible compiler which can be used to install lua(JIT)? or another better scripting language. I wrote a little script idempotently installs lua 5.4 (that I tried to include but HN formatting messed up) and then runs a luascript embedded in a heredoc. The first time I ran it, it took 5.4 seconds to print "Hello, world!" including downloading the lua source code from their website and compiling the whole thing. The second time it took 11ms. I really see no great reason to continue using sh for anything new except for the purpose of installing something else.
- SomeCallMeTim 3y ago"Install something else" is ... a nice idea? But it's simply not realistic. Examples: - Embedded devices that may or may not have an internet connection - Government classified networks that aren't allowed to grab ANY random software off of the internet - Simply not wanting to add another dependency to an operation. I'm a former Lua fan. I've ... written it off at this point. [1] Honestly if I were to take the approach you describe, I'd likely instead have the shell script only look at the architecture and then choose one of 2-3 precompiled Go binaries. Talk about simple and fast...and the binaries could be hosted behind a firewall somewhere. Or maybe I was thinking of it wrong. Just embed the various Go binaries into a tar file embedded right in the shell script[2], and choose the right one based on architecture. Done. :) [1] https://realmensch.org/2016/05/28/goodbye-lua/ https://realmensch.org/2016/05/28/goodbye-lua/ [2] https://www.xmodulo.com/embed-binary-file-bash-script.html https://www.xmodulo.com/embed-binary-file-bash-script.html
- nextos 3y agoBash and most alternatives are full of footguns. However, they are really convenient to glue together different components, e.g. a data preprocessing system. Personally, the way I avoid footguns is to use a functional style. Filter instead of conditions and map instead of loops. GNU Parallel makes this straightforward, including distributed commands across machines! Aside, it is easy to emulate relational algebra with grep, sed, and awk. A good essay describing some of the advantages of this approach is: http://widgetsandshit.com/teddziuba/2010/10/taco-bell-programming.html http://widgetsandshit.com/teddziuba/2010/10/taco-bell-progra...
- usernew 3y agoBash is a shell. A bad shell. Korn is a good shell. It's not meant to be a real language. It's meant to be a shell. Here, let me back that up and reverse it for you. <JAVA> public Set<String> listFilesUsingJavaIO(String dir) { return Stream.of(new File(dir).listFiles()) .filter(file -> !file.isDirectory()) .map(File::getName) .collect(Collectors.toSet()); } //I'm not handling errors, and this doesn't actually print anything, it just lets me try to. <SHELL> ls Shell is meant to be a common way to do common tasks on commandline, and make a little script if you want to save that and run it again. I can do little things in a shell one-liner quick, that would take a page of python or perl. I put up with my sports car because there's no better alternative. It doesn't mean I should put up with a sports car for transporting my piano, just that I must put up with it at least to the point where the sports car can transport me to the car that transports a piano.
- SomeCallMeTim 3y agoBash is used as a programming language. And it's an objectively awful programming language. Sh is even worse. Korn isn't installed absolutely everywhere like Sh is. It's not even installed in 1/100 of the locations that Bash is installed. So it being better is irrelevant to the discussion of Bash as a programming language. Because Bash is so awful, the only real justification for using it as a programming language is that it's already installed. Since Korn isn't already installed, that means you'd need to install it. If you're going to be installing something, there are far better choices. So your comment doesn't at all contribute to the discussion of alternatives to using Bash as a programming language. And the verbosity of using Java to list a folder contents is equally irrelevant. No one would argue that Java is better ... for listing directory contents. If you do want to write a script that calls a bunch of shell commands, or even that operates on folders and files, there are far better languages and tools. See, for example, https://www.npmjs.com/package/tish https://www.npmjs.com/package/tish Or just Node/TypeScript in general.
- nine_k 3y agoThe main use case of a shell is interactive use. There is no reason to invoke a teanspiler when you type a command to be immediately executed. Often its syntax is a bit awkward because in needs to be compact and quick to type. If you want a better language for complex scripts, use Python, or even Perl, they are now ubiquitous, or bring someting self-contained like Lua or Janet with you. It would be great to have a different ubiquitous shell language on Unix machines, but it's unrealistic now.
- zamnos 3y agoThere are two places a new shell language needs to be ubiquitous for success. One is installed on systems, which is pretty easy. With today's connectedness, new software is just an apt install, pacman, or curl | bash away. The other, much harder problem is that the people using the system need to know this new shell language. If I have to debug your shell script using Zsh, Csh, or Fish, I'm not going to be happy about it.
- throwaway892238 3y agoBash is a user interface. The fact that you can string together words and symbols and make logic happen is basically just back-porting advanced user functionality into an existing interface. Having said that, Bash is my favorite programming language, because it is so limiting, and simultaneously not limiting, because it wasn't designed to fulfill what A Real Programmer(TM) thinks a programming language should be. A computer is supposed to serve a human. Not the other way around. But modern programmers, being wildly unimaginative creatures of habit, think the user is supposed to serve the computer. That the more you type, and the more hoops you jump through, the more "clever" you are with the "way" you tell the machine to do something, that this is superior. Never mind that the end result of all this incantation and tom-foolery doesn't actually result in a good outcome half the time. That the user is often unhappy with the result, if they even factor into the programmer's thinking at all. No, the true purpose of programming is to gratify the programmer and make the machine happy, not to make a human's life easier. Programming is a mistake. It's a half-measure. A stutter in the evolution of digital computing. We weren't meant to program, we were meant to make a synthetic invention that relieved us of our labor. And what do we have to show for spending 1/3 of our life, and decades of work, for it? A metal and plastic box that can do pretty much exactly the same real work as 30 years ago, but with better graphics, because the hardware people gave us better screens.
- SomeCallMeTim 3y ago> Bash is a user interface. Meh. That part is irrelevant in the extreme to the discussion. Fact is that people write `.sh` and `.bash` scripts to get things done. They use conditional logic and loops and everything. Functionally, it's a programming language with a (useful) REPL. And this discussion is 100% about how bash is as a programming language. > Having said that, Bash is my favorite programming language, because it is so limiting, and simultaneously not limiting, because it wasn't designed to fulfill what A Real Programmer(TM) thinks a programming language should be. Umm... Then, yeah, I have to agree with your self-assessment of not being a "real programmer." Sorry, but it's obviously true. You're a scripter. I've known and worked with many scripters. They can do a lot of valuable work and contribute in a lot of ways to various projects. But they're not programmers. Which is fine. Script away. > But modern programmers, being wildly unimaginative creatures of habit, think the user is supposed to serve the computer. After admitting you're not a programmer, you then go on to demonstrate that you don't really understand how programming works. Which I guess follows? That's so far from the reality, that it's Not Even Wrong. > That the more you type, and the more hoops you jump through, the more "clever" you are with the "way" you tell the machine to do something, that this is superior. Programming languages are exactly the level of complexity needed to, with the least effort and most reliability, craft the code that, literally, civilization relies on. It can't be made simpler. The complexity is inherent in the domain. > Programming is a mistake. It's a half-measure. A stutter in the evolution of digital computing. We weren't meant to program, ... YOU weren't meant to program. You admitted that above. Don't speak for the rest of us. Everything significant in software that has been and will be accomplished moving forward will be created by programmers using programming languages. Your rant against programmers and programming is based on a lack of understanding of what programming really is and how programmers really think. It's not even slightly about being clever. It's about the act of primal creation. We're effectively wizards of the modern world. We don't care at all about making a machine happy; we care about building things that work. That the things embody complexity isn't the goal, but it is a thing of beauty when it all works together elegantly.
- mixmastamyk 3y agoUse fish for interactive & functions, Python for longer scripts.
- SomeCallMeTim 3y agoI don't choose bash because I am unaware of other languages. I choose it because it's already installed everywhere. If I'm going to install a language/shell, I'll likely choose Node, Bun, or Go, depending. Python isn't actually a much better language from a language design standpoint.
- kagevf 3y ago> "space as delimiter" is awful in so many ways Lisp and Scheme would like to have a word . . .
- SomeCallMeTim 3y agoI needed to learn Lisp in college. I hate Lisp. 'nough said.
- deleted 3y ago[deleted]
- Too 3y agoIf running everywhere is actually a constraint (likely it’s not) and transpiling is a consideration, you could use Golang, that statically links and produces standalone binaries that run more or less anywhere. Though if you have a CD pipeline to transpile and deploy such, you likely have enough control over your environment already to have a less constrained choice to begin with. Normally I’m not very fond of Gos verbosity, especially compared to bash, but i take anything over bash, it is just so filled with footguns, every single line has to be super carefully scrutinized. Note that I say this as someone actually quite good at it, not because I don’t understand it. In fact the more you learn, the more scared you get.
- rascul 3y ago> Bash/Sh is an objectively awful programming language. Subjectively, not objectively.
- renox 3y agoYes, yet 'set -x' in Bash is more useful than in any other languages I know. Because Bash is used to call utils so you see the partial trace of your script that you want to see, not the inside of the tools/utils that you don't want to see. In contrast in gdb for C++ you 'step' inside the STL implementation that most of the time you don't care about..
- SomeCallMeTim 3y agoUmmm.... A debugger won't "step in" unless you..."step in" instead of "step over." STL is also no longer a thing. There's only the C++ standard library. So I think you're dating your usage of C++ by ... a decade or more?
- eternityforest 3y agoThere are lots of better alternatives in almost all cases, just not super weird niche stuff. A lot of things that one would use bash for involve servers, and there's Ansible for that, no need to use bash directly most of the time. If it were up to me, distros would include Ansible out of the box. IMHO doing anything at all that's not programmatically repeatable isn't the best practice, so it's not like I want to be logging onto something and writing a quick script in Nano. Outside of servers.... nobody uses Linux except embedded systems and FOSS enthusiasts. Nobody is paying me to do FOSS work, so I can just pretend that Pythonless systems don't exist. Python does have some version compatibility issues, but for the most part it's stable, and at least it will crash with a traceback not a cryptic error code because you used some utility that had some nonstandard syntax for the flags. On embedded, most of what you're doing is probably simple enough that if you do need to use sh you won't absolutely scream.
- tpoacher 3y agoI completely seriously and unironically find bash to be absolutely beautiful. So, no, it's not "objectively" awful. At most it's "subjectively" awful for some people.