News for hackers. Stuff that matters.
Re:Pure Bash Bible by mankyd on Thursday September 19, 2019 @09:14AM (#21015817)
I'm with jmnicolas on this. I've been using bash on and off for years now (approaching decades). I still get it very wrong almost always. It's the only language where this is a persistent problem for me.

Just yesterday I was struck by the difference between if [[ ]]; and if [ ];

I didn't even bother grokking the difference in the end. I simply found something that worked and moved on with my day.

Re:Pure Bash Bible by zests on Thursday September 19, 2019 @09:32AM (#21015968)
Single square bracket conditionals [] are the "older" (POSIX) version but do not have certain features that we would like to use in our shell scripts. Bash uses double square brackets to extend the functionality.

This is my understanding. Possibly not 100% correct but essentially correct enough for me to understand the reason/rationale for the difference.

Re:Pure Bash Bible by joombaga on Thursday September 19, 2019 @10:55AM (#21016761)
You got it! `test` is a shell builtin. `[` is basically syntactic sugar, and is a synonym for `test` with the additional requirement that the last argument must be a closing bracket `]`. `[[` is a shell keyword with more features.
Re:Pure Bash Bible by serhart on Thursday September 19, 2019 @09:34AM (#21015988)
I'm in no way defending bash as a language. There are lots of gotchas and weird constructs. I avoid bash too. It's just that trim function isn't that cryptic or "unreadable" if you know the syntax.
Re:Pure Bash Bible by pretendscholar on Thursday September 19, 2019 @09:39AM (#21016019)
I think the point is that it takes a lot longer pick the syntax up compared to some if/then/for/loop version of trim even if that would be way more verbose.
Re:Pure Bash Bible by serhart on Thursday September 19, 2019 @09:50AM (#21016124)
Yes, but I think that's antithetical to what languages like bash and perl are trying to do. Someone well versed in the language can do some pretty complex operations in a few keystrokes.
Re:Pure Bash Bible by pretendscholar on Thursday September 19, 2019 @12:21PM (#21017707)
which gets back to the original point that these languages have chosen to be more powerful over being more immediately readable. you have to know many more features of the language to understand what is going on compared to colloquially used constructs like if/then or for loops. If you understand english you are a lot closer to understanding the latter rather than the former, more domain specific syntax.
Re:Pure Bash Bible by serhart on Thursday September 19, 2019 @06:11PM (#21021253)
I don't agree at all. I don't find it less "readable". You just have to be used to it. If you understand English, German isn't that hard of a language to learn. Japanese would probably be a little bit harder to pick up though. Doesn't mean Japanese is unreadable
Re:Pure Bash Bible by cblades on Thursday September 19, 2019 @09:51AM (#21016129)
Maybe "unreadable" and "readable" aren't the best ways to approach this conversation.

It's certainly less readable than, say, my_str.strip()

Re:Pure Bash Bible by joombaga on Thursday September 19, 2019 @11:01AM (#21016823)
Are you comparing a call to a definition? `trim_string "$my_str"` is no less readable than `my_str.strip()`.
Re:Pure Bash Bible by mankyd on Thursday September 19, 2019 @12:23PM (#21017732)
I mean, I would say it is. Why are there quotes around the variable? What happens when I remove the quotes?

The quotes don't function like the parens. If this were a two argument function, you wouldn't put one pair of quotes around the whole thing. They're clearly transforming the variable somehow, but I'm not sure how and/or why they're necessary.

Re:Pure Bash Bible by serhart on Thursday September 19, 2019 @09:56AM (#21016187)
Sure, but bash only has certain language constructs. This implementation is using parameter expansion and some other built ins. It's definitely more complex than yours, but that's what bash has to offer.
Re:Pure Bash Bible by jigglesniggle on Thursday September 19, 2019 @09:48AM (#21016105)
I highly recommend running shellcheck on all of your bash code. It is what finally taught me the practical difference between [[ and [. There are some tests that will always pass in [, for example, but work properly in [[; one project never realized.
Re:Pure Bash Bible by mankyd on Thursday September 19, 2019 @10:00AM (#21016221)
My (personal) better recommendation is to avoid bash scripts whenever possible ;)

I generally tell people that, once a script is longer than ~100 lines and/or you start adding functions, you're probably better off with something like Python.

I know that's not a popular opinion with shell enthusiasts, but it's saved me so much frustration both in writing new scripts and coming back to them later for refactoring.

Re:Pure Bash Bible by cauthon on Thursday September 19, 2019 @10:33AM (#21016551)
> I generally tell people that, once a script is longer than ~100 lines and/or you start adding functions, you're probably better off with something like Python.

Agreed. My threshold is usually "when you want to start using arrays or dictionaries". I find bash best for file manipulation and running programs in other languages

Re:Pure Bash Bible by progman on Friday September 20, 2019 @09:58AM (#21026714)
Bash is a typical Unix tool, and Unix follows the KISS principle. Hence, also Bash scripts should be KISS.

If you need >100 lines of code your code is too complex in the script world, and you should split it into several scripts where each does one thing, and that one thing well. Usually, bash scripts are applied with pipes. A sequence like cat x | tr a b | sort >output is much more likely and easy to handle then a single script which does all these things.

KISS with Bash is a very different approach compared to other scripting languages like Python and Perl. There you can write long code easily and conveniently. However, things can get tough when larger scripts need to be maintained.

I consider the examples in the "Bash Bible" a collection of useful black boxes. It's fine if they just work. Regarding the "unreadable" trim_string for instance, if you have problems to understand that code, and you have to change something then you can simply write your own new trim_string script, even in Python or Perl if you like. Pipes work also well with them.

Update: Another advantage of bash and pipes over Python/Perl is that the Unix system can assign each script in a pipe to a separate thread. That means, simple bash scripts with pipes can work _much_ faster than single scripts in Python or Perl.

Re:Pure Bash Bible by tracker1 on Thursday September 19, 2019 @06:30PM (#21021398)
I tend to work in Windows, Linux and MacOS pretty regularly... so I tend to follow a similar approach (node instead of python though). Windows-isms under msys(git) bash in Windows just gets painful to coordinate at times.

I am curious if people will uptake PowerShell Core (though not much of a fan there either).

Re:Pure Bash Bible by shrimp_emoji on Thursday September 19, 2019 @12:07PM (#21017561)
I'm so glad I'm not alone in this.

I never even bothered to learn Bash properly because even that's difficult, and I figured it'd be "good mental money after bad". And, now that Python ships with all distros, I feel even less need to.

I do like Bash's range syntax though:

    for i in {0..5} ; do echo $i ; done
Like Ruby's. It's so nice! D: I lament Python's lack of it.
Re:Pure Bash Bible by yesenadam on Friday September 20, 2019 @08:58AM (#21026170)

    echo {1..5} 
is even nicer. Or

    echo {1..5}{1..5}{1..5}
Re:Pure Bash Bible by dec0dedab0de on Thursday September 19, 2019 @10:29AM (#21016511)
My rule of thumb is to use a real language if there is more than one if fi block.
Re:Pure Bash Bible by jascii on Thursday September 19, 2019 @02:49PM (#21019315)
So, what in your opinion constitutes a "real language", and why?
Re:Pure Bash Bible by dec0dedab0de on Thursday September 19, 2019 @03:29PM (#21019745)
a "real language" would be any programming language that is primarily designed and presented as a programming language.
Re:Pure Bash Bible by jascii on Thursday September 19, 2019 @05:39PM (#21021004)
Nice recursive definition..

The practice of programming in shell languages was well established when Bash was designed and Bash was definitely designed with that use in mind. So by your definition Bash is a "real" programming language.

Lisp on the other hand, was much more designed as a system and formal notation to reason about certain classes of logic problems.. So, Lisp is not a "real" language?

Re:Pure Bash Bible by dec0dedab0de on Thursday September 19, 2019 @09:40PM (#21022556)
Bash is primarily a shell, it even has the word shell in its full name.
Re:Pure Bash Bible by jascii on Thursday September 19, 2019 @11:07PM (#21022976)
And a shell can't be a real language? https://scsh.net/
Re:Pure Bash Bible by dec0dedab0de on Friday September 20, 2019 @09:22AM (#21026362)
a shell can't be a real language?

No, it can not.

Re:Pure Bash Bible by jigglesniggle on Friday September 20, 2019 @01:57PM (#21029472)
By "programming language" he probably means something that has robust flow control mechanisms and metaprogramming facilities.

You can definitely tell there's a different "feel" to bash and Tcl, Python, Perl, Go, etc, yes? Shell languages basically evolved out of batch processing languages that were meant to only run programs in sequence and it shows.

Re:Pure Bash Bible by jigglesniggle on Thursday September 19, 2019 @10:05AM (#21016264)
I agree, but I like phrasing it as "only run commands with substituted arguments." There's some more discussion on this below. :)

In the past I almost exclusively used Python, but I'm starting to like Go.