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.

Pure Bash Bible – A collection of pure bash alternatives to external processes

291 pointsby rrampageover 3 years ago

16 comments

infogulchover 3 years ago
It&#x27;s Bash Month on Hacker News, apparently:<p>My thoughts on writing a Minecraft server from scratch in Bash | <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30347501" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30347501</a><p>Bash Pitfalls | <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30345487" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30345487</a><p>Show HN: Toolbox – A framework for modular Bash scripts | <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30067708" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30067708</a><p>Use Bash Strict Mode | <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30217719" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30217719</a><p>A Wordle clone in 50 lines of Bash | <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30174112" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30174112</a>
评论 #30365153 未加载
jrm4over 3 years ago
As someone who is <i>genuinely</i> a fan of stupid pointless hacks, like Turing Completeness with CSS or Minecraft or whatnot, in my opinion this is definitely <i>the stupidest.</i> Kudos.<p>The entire point of Bash&#x2F;Shell scripting is (or darn it, ought to be) to use it to string together OTHER programs to get stuff done. &quot;Pure Bash&quot; to me is like a bar of soap that never gets wet.
评论 #30368886 未加载
评论 #30366390 未加载
woudsmaover 3 years ago
Nice to see that the author uses Shellcheck. There is a very nice Shellcheck VS Code extension that links examples of good&#x2F;bad practices when writing Bash. I can definitely recommend using it if you’re writing Bash scripts!
usrbinbashover 3 years ago
<p><pre><code> Use #!&#x2F;usr&#x2F;bin&#x2F;env bash instead of #!&#x2F;bin&#x2F;bash. The former searches the user&#x27;s PATH to find the bash binary. The latter assumes it is always installed to &#x2F;bin&#x2F; which can cause issues. </code></pre> And the former assumes that `env` is installed in `&#x2F;usr&#x2F;bin`, so I am trading one assumption for another.
评论 #30371491 未加载
ChrisArchitectover 3 years ago
Plenty of previous discussion:<p><i>1 year ago</i> <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=24827360" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=24827360</a><p><i>2 years ago</i> <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21013150" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21013150</a>
kwertyoowiyopover 3 years ago
I hope to never read or even use such code. I’ll install as many versions of Python as it takes to avoid this fate.
ilyashover 3 years ago
Nice... but how you fix the way something fundamental as the way `if` works in bash? Each `if` is a bomb. `if grep -q ...;then ...;else ...;fi`. Two branches for three possible exit codes. Given a long script and big amount of output, you will miss the error message such as &quot;file not found&quot; and the execution will peacefully continue into the else branch.<p>Things like the above motivated me to create Next Generation Shell.<p>Edit: hint - yes branch, no branch, exception.<p>More about motivation: <a href="https:&#x2F;&#x2F;ilya-sher.org&#x2F;2020&#x2F;10&#x2F;31&#x2F;bash-or-python-the-square-pegs-and-a-round-hole-situation&#x2F;" rel="nofollow">https:&#x2F;&#x2F;ilya-sher.org&#x2F;2020&#x2F;10&#x2F;31&#x2F;bash-or-python-the-square-p...</a><p>Project: <a href="https:&#x2F;&#x2F;github.com&#x2F;ngs-lang&#x2F;ngs" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ngs-lang&#x2F;ngs</a>
tomcamover 3 years ago
Awesome that it was made free on github. Of course I donated.
xiaqover 3 years ago
If you&#x27;re interested in a shell that allows to trim a string without decoding the following:<p><pre><code> trim_string() { # Usage: trim_string &quot; example string &quot; : &quot;${1#&quot;${1%%[![:space:]]*}&quot;}&quot; : &quot;${_%&quot;${_##*[![:space:]]}&quot;}&quot; printf &#x27;%s\n&#x27; &quot;$_&quot; } </code></pre> You may be interested in Elvish (<a href="https:&#x2F;&#x2F;elv.sh" rel="nofollow">https:&#x2F;&#x2F;elv.sh</a>), which simply has a builtin function for trimming strings: <a href="https:&#x2F;&#x2F;elv.sh&#x2F;ref&#x2F;str.html#str:trim-space" rel="nofollow">https:&#x2F;&#x2F;elv.sh&#x2F;ref&#x2F;str.html#str:trim-space</a>
评论 #30368258 未加载
评论 #30367544 未加载
评论 #30371454 未加载
rossyover 3 years ago
These might make your bash scripts a bit faster in Linux, but they will make them <i>a lot</i> faster in Cygwin and MSYS bash in Windows, where process spawning is <i>very</i> slow. Seriously, if there&#x27;s even a chance of someone running your bash script in Windows (you might be surprised - every bash.exe is either Cygwin or MSYS, including the one that comes with Git for Windows,) your Windows users will thank you for using pure bash instead of external commands.
strzibnyover 3 years ago
Great to see more Bash here. I think for me it comes down to size. If I just need to spin up one server, it&#x27;s surprisingly easy (as I show in Deployment from Scratch), but of course if I have to do something more complex, I would prefer Ruby&#x2F;Python. Certainly bookmarking this resource, thanks!
xvilkaover 3 years ago
There&#x27;s interesting cross-platform &quot;shell&quot; that compiles to either Bash or cmd (bat) - Batsh[1].<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;batsh-dev-team&#x2F;Batsh" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;batsh-dev-team&#x2F;Batsh</a>
frazbinover 3 years ago
The manual is pretty good too <a href="https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;bash&#x2F;manual&#x2F;bash.html" rel="nofollow">https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;bash&#x2F;manual&#x2F;bash.html</a>
r3trohack3rover 3 years ago
Dylan is an incredible mind - he’s behind KISS Linux and neofetch. KISS is worth looking into as an alternative to Linux From Scratch - I learned a lot with it.
cferrover 3 years ago
Another way to touch a file:<p>cp &#x2F;dev&#x2F;null file<p>Test if a port is listening without telnet:<p>echo &gt; &#x2F;dev&#x2F;tcp&#x2F;address&#x2F;port &amp;&amp; echo Yes || echo No<p>That&#x27;s not actually a file. It&#x27;s just a path Bash recognizes and is the interface to socket functionality. udp works too.
评论 #30368706 未加载
jteppinetteover 3 years ago
I’m sorry, but I almost spit my drink out at how unreadable the very first example is.<p><pre><code> trim_string() { # Usage: trim_string &quot; example string &quot; : &quot;${1#&quot;${1%%[![:space:]]*}&quot;}&quot; : &quot;${_%&quot;${_##*[![:space:]]}&quot;}&quot; printf &#x27;%s\n&#x27; &quot;$_&quot; }</code></pre>
评论 #30366472 未加载
评论 #30365060 未加载
评论 #30365243 未加载
评论 #30364707 未加载
评论 #30364615 未加载
评论 #30365464 未加载
评论 #30364702 未加载
评论 #30366822 未加载
评论 #30366104 未加载