Looking for opinions here. Is bash simply popular because it is always available and so there's nothing inherent that makes it popular?<p>Or, is there something special that it has? Writing a bash script seems very different to the rest of the programming I do. And not often in a better way.<p>What are the most interesting, alternatives for simple scripts.
Bash is the language used to express the unix philosophy: <a href="https://en.wikipedia.org/wiki/Unix_philosophy" rel="nofollow">https://en.wikipedia.org/wiki/Unix_philosophy</a><p>It is very much worth a read.<p>Besides the idea that command line programs are written primarily for the purpose of being composable, and that they follow a philosophy that makes them easy to work together, I think there are a few other pieces of secret sauce.<p>The lack of boilerplate is a major advantage. Low boilerplate bash pipes means input/output to/from another program is trivial. Exit code behavior as well as error behavior (output to stderr) further enhances the ease of composing individual tools to do something that would take a while to write a script to do.<p>The power of bash is that just about anything is trivial if it has the form:<p><pre><code> get_data | (map/filter |)* | perform_action
</code></pre>
Bash can be an optimal tool for just about anything that does not require "joining" different types of data or "branching" behavior.<p>Once you invoke branching, joining, or complex data structures/formats, bash is no longer the right tool and python is probably the right one (although if ruby had come first, it is actually probably better for relatively simple scripting).
The author of Oil Shell has some of the best writing on why unix shells are an important niche to push forward.<p>Unix (and later the internet), by enforcing "narrow waists" [1] like a flat stream of octets for all communication, is inherently <i>polyglot</i>.
You can have richer data types inside programming languages, and indeed Python, JS, Rust etc. all have vibrant ecosystems of packages with rich internal APIs. But at some point you want to interface software written in widely different languages, and Unix encourages a particular style of command-line interface which is "dumber" — array of arguments, flat file(s) I/O — but is easy to compose across languages.<p>Therefore, whatever you do in "better" languages, there remains a need for a glue layer whose main job is not doing computation itself but invoking external processes.
There is tons of arguing about the threshold — whether a particular task is better expressed inside a "rich" language or by plumbing together commands — but it doesn't change the need for a good shell language to exist.<p>Now, is bash a great shell language? NO. It's a pragmatic choice for historic reasons. One day we'll do much better. [2]<p>[1] <a href="https://www.oilshell.org/blog/2022/03/backlog-arch.html" rel="nofollow">https://www.oilshell.org/blog/2022/03/backlog-arch.html</a>
[2] <a href="https://github.com/oilshell/oil/wiki/Alternative-Shells">https://github.com/oilshell/oil/wiki/Alternative-Shells</a>
> Is bash simply popular because it is always available and so there's nothing inherent that makes it popular?<p>Exactly.<p>> [...] And not often in a better way.<p>Compared to a fully-fledged programming language? I sure hope so.<p>> What are the most interesting, alternatives for simple scripts.<p>For simple scripts, there's no better alternative. ZSH has some features that you wouldn't find in bash at the time.<p>The safest approach is to use bash or shell (/bin/sh). For programming languages golang binaries are portable, that's the big advantage over python, ruby, perl, etc.<p>When you need to perform advanced error checking, etc. makes sense to use a programming language. For simple operations bash is great, assuming one understands how process ripping works, signal propagation etc. works.
"Scripting" language is a misleading way to view Bash.<p>It's a popular "command" language. PowerShell's language is another "command" language, but Bash is more popular than PowerShell due to its vastly longer history and Unix's superiority as a platform for technical work compared to Windows.<p>Other "scripting" languages (like Python or JavaScript or Ruby) are <i>not</i> drop-in replacements for Bash or PowerShell. You can do with Python what you <i>should</i> do with Bash (such as a nightly system maintenance script or a script that installs a program on your system), but it will be more work (assuming you've already paid the not-insignificant upfront cost to learn Bash). Bash directly interfaces with the system shell in ways Python cannot and was not meant to. Python's a general-purpose scripting language, meant for general work on any system, while Bash was designed to be your gateway to the Bash shell (the terminal) for your Unix system, same as PowerShell for Windows.<p>Arguably, on the flip side, that's why it's so horrible for general purpose work compared to Python. Making a Space Invaders game in Python? Great. In Bash? Morbidly cool but horrible.<p>There are Python-based shells for Linux, for example, but I don't think Python can ever be as ergonomic of a "command" language as Bash since they're two different paradigms. They just happen to both output script artifacts.
It's always available, across time and distros. Despite it being a bit aged, it works well, and even though process management is a pain, it does many things right. There just hasn't been a strong need for a new terminal scripting language when you can easily write a few lines in Python instead when you need to do more advanced things.<p>After a while you will appreciate that the script you wrote 10 years ago still works everywhere, and that new shiny thing that was supposed to replace it died and withered away instead.
I've written a fair number of long bash/zsh scripts for text file manipulation and I loathe to maintain them. They got extra complicated bc they would handle whether you called them directly vs in a subshell, and I always have to refer to a cheatsheet to get the right syntax for anything, since bash is so "powerful" (sensitive to variable expansion or not, etc etc).<p>I tried Ruby instead and it's nice but not enough of an improvement over bash for me. I tried Julia which has nice support for shell but the JIT compilation was annoying so I dropped it.<p>Currently I'm trying Nimscript, which is ok but example code is sparse, docs unfriendly, and the language is hostile to tabs and it has noisy output that you have to disable. Overall it's not pretty but workable so far.<p>Before you downvote me for insulting your fave language, he asked for opinions, and that's what I wrote.
If you want to run some programs and change flow based on what they do why wouldn't you use language that is all about running programs? That is what bash is good at less so other other bits of programming.<p>As a contrast I have been experimenting with using Lua and lua-posix to write a wrapper around ffmpeg. Just some batch encoding tasks nothing too complicated. The logic is easy, spawning the main encodes isn't difficult, reading the json output of ffprobe is hard.
Why not just write something in Python or C#? I don't see a purpose to learning an auxiliary language like Bash anymore when modern high-level languages are so ergonomic.
Perl: <a href="https://www.perl.org/" rel="nofollow">https://www.perl.org/</a><p>Like Bash, Perl is also widely available, more concise and faster than Python, and more of a full programming language than Bash.<p>People love to 'hate on' Perl, but how many of them have actually used it?<p>Most Linux and Unix systems include Perl by default and Perl-compatible regular expressions are the defacto default. For quick and dirty, non-Bash scripts Perl is an excellent choice.
The thing that always irritates me about bash is trying to use nulls as separators so you can run over many files without breaking on spaces or other characters in the filenames.<p>Is it -0 --null, -zero or -z?? Who knows? And every app has a different option. Only a grizzled old greybeard would remember.
It's because you can send it to someone else and virtually guarantee that it runs on their MacOS or Linux machine. Extend that to servers and containers.<p>Things like python, perl, etc are all "nicer" programming languages, but have a lot more issues with distribution. Bash (and related shells) is just a bunch of syntax around setting up environment variables and running commands, which are just executables.
> Or, is there something special that it has?<p>*nix shell is an interactive, concatenative string language with transparent file system access and binary loading capabilities. It's fairly unique among languages and very good at what it does. I would hate to have to write a python script or C program every time I want to rename 1000 files or repack an archive or execute a few programs in sequence or conjuction.
sh shell is specified in POSIX, and it's 'always' there, but it's simply not as usable as bash. It's easier to start as a sh script and give into make it a bash script than to switch to something else later.<p>Perl has a reputation, and isn't always available. Python isn't always available either and has the 2 vs 3 problem still (maybe not for much longer).<p>Many scripts are super simple, or at least start that way, so something that starts as just a list of commands to run is a better interface; but it gets unweildy if you need deeper logic and nesting of things. Such is life.
It's a language that doubles as UI (user interface).<p>Much can be accomplished with 1 liners that are easy to type. That can't be done with something like Python.