Not sure I’d call what bash has functions. They’re closer to subroutines in Basic than functions in other languages, as in you can’t return a value from them (they can only return their exit code, and you can capture their stdout and stderr). But even then, they are full subshells. It’s one of the reasons I don’t really like Bash, you’re forced into globally or at least broadly-scoped variables. Oh, and I have no clue right now how to find where in your pipe you got a non-null exit code.
It’s not a big problem for simple scripting, but it makes things cumbersome once you try to do more.
Functions are definitely not subshells in Bash, seeing as anything modifying the environment, like pyenv and such, is implemented as functions instead of scripts — specifically because functions are run in the same shell instance.
Unless ‘subshell’ means something in the vein of ‘like a new shell, but not really’.
I really like bash when dealing with even somewhat advanced scripting. Like the 300 LOC scraper I have written over the past two days which horribly parses HTML files using grep | sed.
It’s genuinely so much more fun to do this with Bash than, say, Python. I have once written a scraper using Beautifulsoup and I have no desire to do so ever again.
Honestly, only Haskell manages to beat Bash in how satisfying it feels when you manage to get something working well.
Not sure I’d call what bash has functions. They’re closer to subroutines in Basic than functions in other languages, as in you can’t return a value from them (they can only return their exit code, and you can capture their stdout and stderr). But even then, they are full subshells. It’s one of the reasons I don’t really like Bash, you’re forced into globally or at least broadly-scoped variables. Oh, and I have no clue right now how to find where in your pipe you got a non-null exit code.
It’s not a big problem for simple scripting, but it makes things cumbersome once you try to do more.
Functions are definitely not subshells in Bash, seeing as anything modifying the environment, like
pyenvand such, is implemented as functions instead of scripts — specifically because functions are run in the same shell instance.Unless ‘subshell’ means something in the vein of ‘like a new shell, but not really’.
I really like bash when dealing with even somewhat advanced scripting. Like the 300 LOC scraper I have written over the past two days which horribly parses HTML files using grep | sed.
It’s genuinely so much more fun to do this with Bash than, say, Python. I have once written a scraper using Beautifulsoup and I have no desire to do so ever again.
Honestly, only Haskell manages to beat Bash in how satisfying it feels when you manage to get something working well.