TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Case for Bash (2021)

125 pointsby memalignabout 2 years ago

32 comments

SomeCallMeTimabout 2 years ago
Bash&#x2F;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 &quot;space as delimiter&quot; is awful in <i>so many ways</i>.<p>But especially Sh runs everywhere, so it&#x27;s good to know. This seems to be the article&#x27;s point.<p>Honestly, though? The right answer is to transpile a better language to Sh.<p>Looking around, there have been several half-hearted, abandoned attempts. But apparently everyone is happy to just fall back on the &quot;accidental syntax&quot; of Bash that really doesn&#x27;t make sense when compared to ... any other language.<p>I put up with Bash because there&#x27;s no better alternative. That doesn&#x27;t mean we <i>should</i> put up with Bash; just that we <i>must</i> put up with it at least to the point where it can run code written in a <i>real</i> language.
评论 #35997050 未加载
评论 #35996714 未加载
评论 #35995195 未加载
评论 #35997007 未加载
评论 #35996533 未加载
评论 #35995500 未加载
评论 #35996571 未加载
评论 #36002646 未加载
评论 #35998110 未加载
评论 #35997725 未加载
评论 #36003160 未加载
评论 #36001939 未加载
评论 #35995174 未加载
评论 #36000785 未加载
评论 #35997442 未加载
评论 #35995415 未加载
augustkabout 2 years ago
This article is not specifically about Bash, but about the shell command language sh. The POSIX Shell is a standardized subset of Bash which has even better portability. I would recommend learning and using sh unless you really need Bash.<p><a href="https:&#x2F;&#x2F;pubs.opengroup.org&#x2F;onlinepubs&#x2F;9699919799&#x2F;utilities&#x2F;V3_chap02.html" rel="nofollow">https:&#x2F;&#x2F;pubs.opengroup.org&#x2F;onlinepubs&#x2F;9699919799&#x2F;utilities&#x2F;V...</a><p>Your script will use sh if it starts with #!&#x2F;bin&#x2F;sh (instead of #!&#x2F;bin&#x2F;bash).<p>I can also recommend ShellCheck which is a shell script analysis tool (implemented in Haskell) which can find errors and potential problems in your scripts. On a Debian system, installing ShellScheck is as simple as `sudo apt install shellcheck&#x27;.
评论 #35986327 未加载
评论 #35986467 未加载
评论 #35989626 未加载
评论 #35986767 未加载
评论 #35986654 未加载
评论 #35994861 未加载
评论 #35989671 未加载
评论 #35989868 未加载
cwingravabout 2 years ago
BASH is like Obi Wan. It isn’t the most powerful or flashiest, but it survived a long time, where others didn’t, for very good reasons. Bash runs basically everywhere. It has many modern features you wouldn’t expect. Its syntax is literally what you would type on the command line if you were diagnosing or fixing systems so you don’t need to transpile to another language. Its reliance on other programs means it is glue and can easily incorporate highly cohesive functionality&#x2F;tools others write and maintain. Also, it’s been around and is everywhere so you don’t worry about trying to incorporate the current latest and greatest declarative tool (which will blow over in 5 years) into your other workflows. Basically, don’t disparage a Jedi&#x2F;tool that has survived where others didn’t. There is a reason.
评论 #35986296 未加载
评论 #35992046 未加载
pjc50about 2 years ago
The only real advantage of bash (or other shells) is the ability to build up to it from the command line; to use it as a REPL then embed the stuff you&#x27;ve just done into a script.<p>The massive disadvantage of bash is that it uses a legal filename character &quot; &quot; as a delimiter (&quot;$IFS&quot;). That means it&#x27;s very easy to write scripts which work fine on your test case but blow up in a different context. And the worst case of that is accidentally deleting the user&#x27;s file system.<p>So many of the cute little bash examples you&#x27;ll see, including I think the awk one on this page, will go wrong if you have a space in the wrong place in your filesystem. Or, god help you, a newline. Or a file named &quot;*&quot;. Or &quot;--&quot;.
评论 #35986213 未加载
评论 #35986272 未加载
评论 #35986161 未加载
评论 #35989977 未加载
评论 #35989572 未加载
makzabout 2 years ago
I have a counterpoint for many comments here. If you are writing some Python for example, and most of your code is executing commands such as tail, cat, wc, etc. and then doing something with their output, you are better off writing a shell script instead .
评论 #35996758 未加载
todd8about 2 years ago
I&#x27;m a computer scientist&#x2F;software engineer that has used primarily Unix or Linux systems since the 80&#x27;s. I write shell scripts occasionally, but I just find them error prone enough that I&#x27;d rather use python than sh for complex scripts. Python has some packages that make working with files, directories, and processes tolerable and at least I can understand the string quoting rules in python.<p>Generally my order of preference for shell scripting tasks is: python &gt; bash &gt; pearl &gt; tcl &gt; sh &gt; anything &gt; AppleTalk.
评论 #35995934 未加载
stanleydrewabout 2 years ago
This argument isn&#x27;t very compelling. Shell scripts are great at tricking you into believing you&#x27;ve got something 100% working when in fact you&#x27;ve only accounted for the happy path and maybe two or three failure cases. This is the primary reason shell scripts are so often broken. There are tons of assumptions baked in which are easily violated.<p>If your shell script keeps breaking, and you keep fixing it by writing more shell, then you&#x27;re in an abusive relationship with your programming environment.
评论 #35998351 未加载
badrabbitabout 2 years ago
I feel like zsh is for pros maybe. I use Linux daily and write python and shellscripts. Zsh has been defaulted all over the place but all that meant for me is I have to figure out how to get it to do things the bash way once in a while.<p>I see no reason why distros like ubuntu or kali would default to zsh.I use the shell for sysadmin stuff, anything complex or app specific gets at least a python treatment.<p>The whole thing reminds me of python 2-&gt;3 or sysv init to systemd. There must be people somewhere deeply invested in these changes and their voice is certainly much more valuable than the average opensource joe user. Defaults matter and the time I and millions had to spend learning to transition for no direct benefit to us has value. The whole distro model of everyone getting a distro they like falls apart when not enough people can fork.<p>Overall, this devalues opensource as an investment. Because of unpredictable hidden costs like this.
rfittichabout 2 years ago
I think this perspective is essential in our ever-evolving tech industry. Bash, despite its quirks and often steep learning curve, has its niche in our toolset.<p>Its power lies in its simplicity and direct access to system level functions, which makes it an efficient tool for system administrators and developers working in DevOps. It&#x27;s excellent for automating small to medium-sized tasks on UNIX-like systems where the overhead of a full-fledged language would be overkill.<p>While it&#x27;s true that bash has its limitations, the value of its ubiquity cannot be overstated. The bash shell is everywhere - from massive server clusters to tiny embedded systems. This means that a bash script written on one system is very likely to work unchanged on another.
评论 #35990372 未加载
JdeBPabout 2 years ago
The article is a bit confused about whether it&#x27;s making a case for shell scripting in general, or for the Bourne Again shell in particular. By the looks of things, its target readership is people who don&#x27;t use shell scripting, or interactive shells, <i>at all</i>; so the former case would be more appropriate, and the drawing of a distinction between the Bourne Again, Friendly Interactive, Z, and other shells just muddies the waters for that readership.
sigmonsaysabout 2 years ago
it seems like most people that have dislike bash never spent the time to learn it.<p>I have a love&#x2F;hate relationship with is. I started with computers in 2000 and wrote a ton of bash for various things. Compiling software, running cicd, adhoc batch jobs, etc.<p>I&#x27;ve managed many cicd pipelines with bash in my time. While it&#x27;s not great, once you know the basics, you can be very successful. That being said, rewriting the bash &quot;program&quot; in another language is much advised. If your bash is over a few hundred lines, chances are you&#x27;ve outgrown it<p>Some things in bash (mainly pipes and redirection) are just too easy. Job control is also great. The &#x27;process&#x27; and &#x27;shell script&#x27; are two primitives that make a lot possible.<p>Trying to do pipes or redirection in python is awful. There are a ton of subtle bugs you have to worry about
评论 #35995050 未加载
faeriechanglingabout 2 years ago
The thing that makes me loathe Bash more than anything else is it’s 3 types are string, int, and list and those are not the only types of data I work with day to day.
jquastabout 2 years ago
We should have better local shells that “drive” remote sh&#x2F;bash shells through transpilation, so that we can have modern shells anywhere without concern for remote install, access to install or compile, compatibility, carrying customizations, etc.
vram22about 2 years ago
IMHO, the best resource to learn about both Unix shell usage at the command-line prompt, and shell programming in .sh script files, is still the book &quot;The Unix Programming Environment&quot; by Kernighan and Pike, except for shell gotchas and subtleties which were discovered later.<p>Of course, they still would not have covered all possible issues known even at that time, because that was not the focus of the book, which was to be a Unix tutorial, not just on shell, but many other commands, general Unix usage, and the environment too (hence the title of the book).<p>But they did cover and warn about some issues, including giving solutions.
jagged-chiselabout 2 years ago
&gt; It coordinates programs…<p>Sounds like a case for shells in general. No other script-execution tools make running other programs (and linking their inputs and outputs) so simple.<p>To overcome Bash’s problems, one can choose another shell. But I suppose you can’t just pick your personal favorite and script in its dialect, because then how would the human tasked with integrating the script into $wherever know which shells to have installed? You can stick with sh which is guaranteed to be installed, or bash (indeed an improvement over sh) which is nearly guaranteed to be installed.
评论 #35986347 未加载
评论 #35986300 未加载
BoardsOfCanadaabout 2 years ago
Maybe this already exists, but I imagine if someone wrote at library for interacting with gnu or posix commands from python (or ruby, or...) that built up the commands in a structured way and parsed the replies and put them in a dict, that would be very nice to use instead of bash-scripts. Every weird thing that you wanted to do would be a lot easier to do in python than in bash, and none of that looking out for spaces in strings and so on.
评论 #35992380 未加载
评论 #35991617 未加载
评论 #35998175 未加载
BirAdamabout 2 years ago
I’ve written a ridiculous number of shell scripts to automate many many things. It’s great for doing something now, and it’s great as a way to interact with a machine. It is not great in all cases. Particularly: array handling is bad, it’s slow if you start using actual Bashisms, very few younger devs seem to know it.<p>The shell is great cuz it’s there and it can cover about 50% of use cases for server side automation, but it’s bad for the reasons mentioned above.<p>I love it and I hate it.
frumiousircabout 2 years ago
Am I missing something or is the article&#x27;s central example using `tail` fundamentally broken?<p>The `sort` blocks until input is closed yet `tail -f` never closes its output.
评论 #35995629 未加载
评论 #35986449 未加载
评论 #35986436 未加载
gtsopabout 2 years ago
As a serious programmer, I have printed out and read most of the bash v5 manual.<p>As a serious programmer, i do TDD even with bash, it is called bats-core<p>As a serious programer I use the right tool for the right job, so I write bash scripts when it makes sense.<p>Having done all these, I rearly write bash programs, but when I do, it is a joy! Plus i have gteater terminal understanding which is useful beyond bash programming.
lloekiabout 2 years ago
&gt; The Python and Ruby versions would be similar<p>Not Ruby!<p><pre><code> sh = Shell.cd(&#x27;&#x2F;&#x27;) sh.transact do system(&#x27;tail -fq &#x2F;var&#x2F;log&#x2F;nginx&#x2F;access.log&#x27;) | system(&quot;awk &#x27;{print $7}&quot; | system(&quot;sort&quot;) | system(&quot;uniq -c&quot;) | system(&quot;sort -rn&quot;) end </code></pre> Pretty sure one could get without the system method with Shell having def method_missing(meth, * args) to call system([meth.to_s, * args]) or something, turning it into:<p><pre><code> sh.transact do tail &#x27;-fq&#x27; &#x27;&#x2F;var&#x2F;log&#x2F;nginx&#x2F;access.log&#x27; | awk &#x27;{print $7}&#x27; | sort | uniq &#x27;-c&#x27; | sort &#x27;-rn&#x27; end </code></pre> <a href="https:&#x2F;&#x2F;github.com&#x2F;ruby&#x2F;shell#pipe-etcprintcap-into-a-file">https:&#x2F;&#x2F;github.com&#x2F;ruby&#x2F;shell#pipe-etcprintcap-into-a-file</a>
SPBSabout 2 years ago
So it&#x27;s both ubiquitous and a terse DSL for managing and coordinating programs. Yeah I agree. Also if you&#x27;re on Windows it&#x27;s nice that PowerShell is always preinstalled and serves the same purpose.
owenpalmerabout 2 years ago
1. Someone creates a platform.<p>2. They make a terrible language the most convenient language.<p>3. People use the platform, along with the terrible language.<p>4. This terrible language is now everywhere.<p>5. The argument for using this terrible language is #4
andrewlabout 2 years ago
The article recommends <i>Bite Size Bash</i> by Julia Evans. Does anybody have an experience with it?
评论 #35996986 未加载
amadeuspagelabout 2 years ago
&gt; This is a contrived example<p>Indeed, in JS you wouldn&#x27;t use awk and sort, you&#x27;d use JS array functions. It&#x27;s fine to have examples that are contrived in the sense that the problem is contrived, but the solution should not be.
pgtanabout 2 years ago
Well, Kornshell (ksh93) has some nice programming features, much better than bash.
shmerlabout 2 years ago
Good points. But I wish Bash had better syntax and built in support for basic concepts like boolean logic. Try to compose some complex boolean expression in Bash and you will quickly hit problems.
majkinetorabout 2 years ago
Quality of the articles on the front page seems to be rapidly declining...
评论 #35986013 未加载
lekeabout 2 years ago
I was just thinking about whether or not there is a compile to bash language.
jononomoabout 2 years ago
I have found that GPT-4 can write bash scripts for me pretty well.
DonHopkinsabout 2 years ago
The only thing you need Bash for is to install Python.
deafpolygonabout 2 years ago
Bash is a shell. Learn the shell, and you &quot;learn&quot; bash.
评论 #35986374 未加载
msoadabout 2 years ago
I ask chatgpt for bash scripts. So useful and every time I’m learning something new
评论 #35986247 未加载