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/Shell scripting is (or darn it, ought to be) to use it to string together OTHER programs to get stuff done. "Pure Bash" to me is like a bar of soap that never gets wet.
Nice to see that the author uses Shellcheck. There is a very nice Shellcheck VS Code extension that links examples of good/bad practices when writing Bash. I can definitely recommend using it if you’re writing Bash scripts!
<p><pre><code> Use #!/usr/bin/env bash instead of #!/bin/bash.
The former searches the user's PATH to find the bash binary.
The latter assumes it is always installed to /bin/ which can cause issues.
</code></pre>
And the former assumes that `env` is installed in `/usr/bin`, so I am trading one assumption for another.
Plenty of previous discussion:<p><i>1 year ago</i> <a href="https://news.ycombinator.com/item?id=24827360" rel="nofollow">https://news.ycombinator.com/item?id=24827360</a><p><i>2 years ago</i> <a href="https://news.ycombinator.com/item?id=21013150" rel="nofollow">https://news.ycombinator.com/item?id=21013150</a>
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 "file not found" 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://ilya-sher.org/2020/10/31/bash-or-python-the-square-pegs-and-a-round-hole-situation/" rel="nofollow">https://ilya-sher.org/2020/10/31/bash-or-python-the-square-p...</a><p>Project: <a href="https://github.com/ngs-lang/ngs" rel="nofollow">https://github.com/ngs-lang/ngs</a>
If you're interested in a shell that allows to trim a string without decoding the following:<p><pre><code> trim_string() {
# Usage: trim_string " example string "
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
</code></pre>
You may be interested in Elvish (<a href="https://elv.sh" rel="nofollow">https://elv.sh</a>), which simply has a builtin function for trimming strings: <a href="https://elv.sh/ref/str.html#str:trim-space" rel="nofollow">https://elv.sh/ref/str.html#str:trim-space</a>
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'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.
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'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/Python. Certainly bookmarking this resource, thanks!
There's interesting cross-platform "shell" that compiles to either Bash or cmd (bat) - Batsh[1].<p>[1] <a href="https://github.com/batsh-dev-team/Batsh" rel="nofollow">https://github.com/batsh-dev-team/Batsh</a>
The manual is pretty good too <a href="https://www.gnu.org/software/bash/manual/bash.html" rel="nofollow">https://www.gnu.org/software/bash/manual/bash.html</a>
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.
Another way to touch a file:<p>cp /dev/null file<p>Test if a port is listening without telnet:<p>echo > /dev/tcp/address/port && echo Yes || echo No<p>That's not actually a file. It's just a path Bash recognizes and is the interface to socket functionality. udp works too.
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 " example string "
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}</code></pre>