Re:What the GNU?
by forgotpwd16 on Thursday September 30, 2021 @10:36AM
(#28706830)
There're many features that are useful for programming: variable expansion, pattern replacement, substing expansion, and others. Not only that but some bash built-ins essentially replace external processes leading to your script being faster and more portable (ironically).
[ Parent
| on HN ]
Bash is not alone in having built-ins that make it faster. Generally speaking, Bash is measurably slower than other shells. If you convert make your shell scripts portable and change #!/bin/bash to #!/bin/sh, in my experience, this usually makes them faster, too (on systems where sh is not bash). This is the driving factor for Debian's switch from Bash to Dash, which resulted in noticeable startup speed improvements:
https://wiki.ubuntu.com/DashAsBinSh
(Note that Systemd has since obviated most of that work.)
You may be writing very different shell scripts than the ones which I encounter. I'm not sure what you mean by "variable expansion", because variable expansion is portable. Something like 98%+ of all pattern replacement I see is something simple like replacing a suffix or prefix of a string. For example,
infile=input.png
outfile="${infile%.png}.jpg"
You can typically use sed for the complicated stuff.Above a certain level of complexity, I'd likely rewrite the script in Perl or Python, anyway. Or possibly Go.
Re:What the GNU?
by forgotpwd16 on Thursday September 30, 2021 @03:26PM
(#28710255)
For your example, in bash you can do
infile=input.png
outfile="${infile/.png/.jpg}"
Yes, supposing it exists and you've permissions for it, you can use sed. Do that a dozen times and the calls to an external process add up to a significant time. But the real advantage is being easier to write and work with.