How about you don't? Bash as scripting language is rather mediocre. Anything that is not simple in bash gets hard to read and debug and probably is wrong on some subtle levels. I have a rule of thumb that any shell script that grows beyond a screenful of lines gets redone in a proper scripting language.
Bash as a scripting language is actually pretty amazing. It gives you everything you need to perform some quick-and-dirty tasks with minimal overhead. If you need only work on sequential data, files and processes, it's a perfect match. It's not a full-fledged programming language by any stretch of the imagination (lacking structures more complex than associative arrays), but it's damn good for scripts of all sorts. A…
Ask HN: How can I get better at bash?
51–60 of 195 posts
Re: Ask HN: How can I get better at bash?
#52Figure out a problem and try solving it in bash - bash for beginners guide on tldp site can get you started. You get better as you use it. http://www.tldp.org/LDP/Bash-Beginners-Guide/html/ EDIT : Additional links - Advanced - http://tldp.org/LDP/abs/html/ Bash programming - http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
ABSG is always my answer to how to learn shell
Re: Ask HN: How can I get better at bash?
#53Figure out a problem and try solving it in bash - bash for beginners guide on tldp site can get you started. You get better as you use it. http://www.tldp.org/LDP/Bash-Beginners-Guide/html/ EDIT : Additional links - Advanced - http://tldp.org/LDP/abs/html/ Bash programming - http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
ABSG is always my answer to how to learn shell
Re: Ask HN: How can I get better at bash?
#54Re: Ask HN: How can I get better at bash?
#55I used to lean on python for much of my scripting needs, mainly because the more advanced bash syntax was pretty daunting. Getting better at bash has a trickle-down effect, especially in this container age. ENV var scoping + loops and various var expansion methods really made it click for me. Shelling out to various tasks (and grabbing the results) is effortless via bash scripts. With bash on windows now it's pretty…
In both my personal and professional life, I have the opposite conclusion. Bash is only for the simplest scripts and pipelines, and everything else (assuming you're going to use it more than once) gets written in Python or Go. The Bash syntax is not daunting, or if it is that's never been the problem with Bash. The problem is that Bash or shell programming in general gives you a million ways to shoot yourself in the…
Maybe I'm overlooking something...but why wouldn't this work:
for file in $(ls); do {}; done
Re: Ask HN: How can I get better at bash?
#56Re: Ask HN: How can I get better at bash?
#57Earlier quoted context omitted.
OTOH, if you can structure your problem as a composition of pipelines, it can be quite a bit faster in bash than in a "proper" language. You get to choose optimized tools, and they run concurrently. Writing efficient bash code forces you to think about your problem differently. It's a very similar process to thinking functionally; e.g. you don't want to deal with lines of a file one at a time in a loop, you want to d…
Is there a "proper scripting language" somewhere that supports the same first-class access to Unix programs and the same piping | syntax that shells in general do? I would love something like that.
use IPC::Run qw(run);
IPC::Run::run(["echo", "abc"], "|",
["tr", "a-z", "A-Z"], "|",
["cat"],
\$out);
print "out: $out";
will yield out: ABC
Useless use of cat here just to make a point, of course.It's easy to use Perl strings as input or output, or files, and there's also ways to interact with streams as they go along.
Generally speaking the slightly more verbose interaction with shell is made up for by the savings when you can use Perl directly to do something rather than spawn a shell process for something simple. (Shell composition can be powerful, yes, but on the other hand spawning a full process and setting up a pipe for things like "tr" or "wc" is often just silly.)
I also personally believe there's a win in the syntax; you may say "What? 'tr a-z A-Z' is way more convenient than '["tr", "a-z", "A-Z"]' but I say one of the biggest and most pervasive errors in shell is to incorrectly set up the arguments by having something interpolated in incorrectly. Having an unambiguous syntax for separating arguments has often made it much safer for me to write the code that has to use the shell. Used correctly it can even be made to work in the face of user-supplied input, something that should generally not be combined with any form of implicit argument separation. (Although bear in mind that "used correctly" encompasses more than just "separating the arguments correctly.")
I am also NOT claiming exclusivity; this is just the thing I know off the top of my head. I'm sure the other scripting languages have nice libraries, too. Though watch out for them being too nice. There's a bit of an impedance mismatch between shell and scripting language. Anything that smooths it over too well is probably either giving up critical features or introducing subtle bugs, which can become security bugs in the face of arbitrary user input.
[1]: http://search.cpan.org/~toddr/IPC-Run-0.96/lib/IPC/Run.pm
Re: Ask HN: How can I get better at bash?
#58How about you don't? Bash as scripting language is rather mediocre. Anything that is not simple in bash gets hard to read and debug and probably is wrong on some subtle levels. I have a rule of thumb that any shell script that grows beyond a screenful of lines gets redone in a proper scripting language.
Also, even if you manage to become better in Bash, you are bound to lose your skills at some point when you have been programming in other languages for a while.
I always have to look up how to do even basic things in Bash. I just don't use it often enough for these things to "stick".
Re: Ask HN: How can I get better at bash?
#59In addition to what others said, I can recommend just reading through the manpage once (probably in multiple sittings). Even if you don't remember the exact syntax, you will have an idea what bash can do, and know enough of the jargon to find it again in the manpage when you need it. For example, when I need to replace a substring, I used to do FOO="Hello World" ... BAR="$(echo "$FOO" | sed "s/World/Hacker News/")" u…
I would also recommend to read Single Unix Specification on shell syntax, and then avoid bashisms whenever possible. Bash had a history of subtly changing its behaviour in these, which leads to scripts getting suddenly broken without any modification on system update.
Re: Ask HN: How can I get better at bash?
#60How about you don't? Bash as scripting language is rather mediocre. Anything that is not simple in bash gets hard to read and debug and probably is wrong on some subtle levels. I have a rule of thumb that any shell script that grows beyond a screenful of lines gets redone in a proper scripting language.