Or we are both the same kind of horrifying :D
However, in cases where portability is important, bash is would be my choice. If I can pass someone a bash script and not ask them to also install Python and whatever libs I used, it makes my life so much easier.
That's a weird claim. Could it be that you have never really learned shell programming or read the man pages? Or can you give an example? We have lot of production code with many test statements and I don't remember when we were bitten last time by some "surprising feature" of test.
I agree that test works differently than comparisions in many programming languages. So you need to understand how it is different. And you need to be disciplined about quoting, otherwise surprises will happen when you have user-controlled input and the shell starts to do word-splitting. But if you use shellcheck you will be reminded of quoting everywhere. It's not a problem specific to test anyway.
Or I can use a sane language that let's me get my stuff done faster and with less opportunities to shoot myself in the foot.
But the tweet quoted in the article rings very true to me:
>The opposite of "it's like riding a bike" is "it's like programming in bash".
>A phrase which means that no matter how many times you do something, you will have to re-learn it every single time.
I and probably a lot of programmers write bash scripts just enough to remember the vague shape of what we want to do, yet still infrequently enough to require looking up the exact characters every single time (-z? ! -z? one bracket? two brackets? quotes? no quotes? something about a semicolon and "then", right? where can you add newlines, again? oh yeah, it's not "end" like Ruby/Lua but "fi" right? what's "else if", again?).
I'm sure if we were working with bash scripts daily, we'd memorize it, but it's just rare and just different enough that it's basically day 1 all over again every time. Shellcheck helps as a safety net, but it doesn't necessarily save me a lot of time and cognitive load.
If I'm writing a new script from scratch and I realize there's going to be some branching or prompts or complex argument handling, I just write it in Python. It saves time and effort, it lets other people on my team more quickly modify and debug it, and it's more predictably portable ("oops, only sh, no bash"). And if it ever needs to do something more advanced in the future, it's way easier to extend.
Or put much more succinctly, by 'kristaps in another comment:
>Or I can use a sane language that let's me get my stuff done faster and with less opportunities to shoot myself in the foot.
It's nice for quickly writing something that's just wrapping multiple commands, though.
Once you get used to using shellcheck all the time your code style starts to match what it requires. You start thinking about quoting and other intricacies, and coding much more defensively.
This happens with other checkers as well - >90%+ of the time the python I write passes pep8 and black cleanly after having used those tools for a year or two.
The issue is the frequency. Due to not coding in it often, you don't retain it.
Probably around once a week.
A linter should make you a better coder, whether or not you code frequently, and also serves as a secondary "belt and suspender" check for code sanity and ease of future reading/refactoring.
really, most of these questions have only one correct answer. [ ! is never correct, neither is ! [ -z or ! [ -n. one bracket in /bin/sh, two in bash. quote every variable expansion unless you have a very good reason not to. add new lines in sensible places. nobody (sensible) complains that you can't write
for
num
in
mylist
:
in Python, so don't write it in shell either.the problem is there is so much cargo culting garbage. the same problem exists in "C/C++", where people write "int* buf = (int* )malloc(num*sizeof(int))" to be "compatible", or do gratuitous pointer aliasing with undefined behavior, despite the fact that any modern C implementation with any optimizations at all will inline small fixed-size memcpy.
I'm thus willing to take the time-hit on rewriting in Python, because I'm betting that resulting code will be easier to adapt and easier to troubleshoot as this extra complexity gradually comes into view.
But my scripts are also either for personal use, or for internal use (5 person team) at work, so I know who will be running the scripts, why, and where (which OS/versions). Suppose it's a bit like developing for Apple where you can remain aware of the use (and edge) cases.
If you expect that someone else can read and maintain your code with minimal learning I don't think it's the tool of choice.
(Fun-for-mostly-me fact, my very first programming job ever was writing awk. It was a while ago. I certainly don't remember any awk now).
This previous comment of mine goes into a bit more detail: https://news.ycombinator.com/item?id=23242753
The first models came even with 4 KB RAM / 3 KB usable. Have never seen one of those, though.
I was talking about embedded Linux here, so 512 MB RAM is not really uncommon, many systems have even more. No problem to run even bash. dash is sometimes preferred for license reasons, busybox ash is smaller, but then programming can probably get more annoying already, because useful features are probably missing. I use dash regularly for scripting (interactive use is a pain), haven't had to use ash for a while.
Of course there are much smaller embedded systems. But I don't think they would run Linux.
[1] https://github.com/landley/toybox/blob/master/toys/pending/s...
usage=$'
[-1][+NAME?script - Script synopsis here]
[f:flag?Some flag description]
[p:param?Some param description]:[string?description]
'
while getopts "$usage" opt
do
typeset opt_${opt}=${OPTARG:-1}
doneAlso pipe/subprocess heavy scripts often become sad complex monstrosities in non-shell languages.
while(<>){
chomp;
<insert your -E code here>
}