Live data from Hacker News

Ask HN: How can I get better at bash?

news.ycombinator.com

61–70 of 195 posts

Re: Ask HN: How can I get better at bash?

#61

I'm ok at bash, but I do not default to complicated bash scripts for my needs. I make little reusable tools. For example I have a tool aliased that makes it easy to apply quick ruby code. For example echo "345.44 544.50" | rg "€#{l}: €#{(l.to_f * 3).to_i}" Produces the following output: €345.44: €1036 €544.50: €1633 Based on this code: https://gist.github.com/zachaysan/4a31386f944ed31a3f8a920c85... I find it's much f…

Any particular reason you don't use `ruby -p`? Looks like it's doing pretty much the same thing.

Re: Ask HN: How can I get better at bash?

#64
post #13

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…

Typically a program like Ansible will have the majority of its use cases implemented in a minority of its code, while the rest of the code will be there to support special cases, edge cases, rare use cases, etc. So that contributes to the disparity in size.

Re: Ask HN: How can I get better at bash?

#65
post #22
post #17

Earlier 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.

Perl was created to do this stuff. Perl is like duct tape to hold together everything. Most people call it ugly because of that but Perl is the best language for quick dirty one liners that work. Only problem is that if you look at it later you will have problems understanding it.

Re: Ask HN: How can I get better at bash?

#66
post #55

Earlier quoted context omitted.

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…

> Like iterating over the files in a directory, for example. If you think that's easy in Bash you have either have a funny definition of easy Maybe I'm overlooking something...but why wouldn't this work: for file in $(ls); do { }; done

  $ mkdir "hi there"
  $ mkdir "how are you"
  $ ls -l
  total 8
  drwxr-xr-x 2 xxxxxxxx xxxx 4096 Jun 26 14:54 hi there
  drwxr-xr-x 2 xxxxxxxx xxxx 4096 Jun 26 14:54 how are you
  $ for f in $(ls); do echo $f; done
  hi
  there
  how
  are
  you

Re: Ask HN: How can I get better at bash?

#68
Read the manual front to back and install shellcheck. Doing both things has paid off for me a thousand times over. The rest is practice. Complete the bash exercises on Hackerrank. Bash is fantastic in it's domain but it does require serious study in my experience
Post reply on HN