News for hackers. Stuff that matters.
Re:Bash functions are better than I thought by chaps on Sunday October 31, 2021 @08:19PM (#29061801)
Isn't grep your friend in this situation? I'm really not sure why you can't have side effects in this scenario.
Re:Bash functions are better than I thought by rablackburn on Sunday October 31, 2021 @08:50PM (#29061976)
A “side-effect” in this case is the ability for a function you call to do something “outside” of itself. For example, to change the value of a global variable.

It’s generally considered best practice (in the functional programming community at least) to write “pure” functions (that is, functions without side effects) because it’s much easier to reason about what they are doing.

So good news, subshells can _only_ be pure (the only way to get anything back from them is if they write some string to stdout), but sometimes you do actually want to have some side-effects (imagine a function that reads a config file and then wants to set some of the global variables to the values found there).

Well, bad luck. If you use the subshell syntax you literally can’t do that.

Re:Bash functions are better than I thought by quotemstr on Sunday October 31, 2021 @08:24PM (#29061832)
What on earth does grep have to do with side effects?
Re:Bash functions are better than I thought by chaps on Sunday October 31, 2021 @08:36PM (#29061904)
And what makes you think side effects aren't possible in the model described?

Think of grep as a way to control flow. If one side effect happens, send to stdout one thing. Another side effect, send something else. Then iterate over those outputs.