Live data from Hacker News

Ask HN: How can I get better at bash?

news.ycombinator.com

11–20 of 195 posts

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

#11
In 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/")"
until I remembered that bash can do string replacement by itself. A quick search for "pattern" and "substitute" in the manpage turned up the right syntax,

  BAR="${FOO/World/Hacker News}"

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

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

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

#14
1. Use it.

2. Conceive of use-cases you can't already solve, and see if you can find a way to do them using Bash.

3. Consider that perhaps Bash isn't the best tool for every job. (It most certainly isn't, though you can abuse it frightfully.)

4. Books. Jerry Peek's guides are getting rather dated, but they're still a good introduction.

5. Read the manpage. Frequently. Find some part of it that doesn't make sense, or that you haven't played with before, and play with it. Shell substitutions, readline editing, parameter substitution, shell functions, math, list expansions, loops, tests, are all high-payoff areas.

6. Take a hard look at zsh, which does a great deal Bash doesn't.

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

#15

In 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'd recommend reading the manpage far more than once.

It runs about 70-80 pages, so counts as a small book.

Hrm. Make that 107 pages:

    man bash | pr | grep 'Page [0-9][0-9]*' | tail -1
46,000 words.

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

#16

In 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?

#17
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.

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 do filters and maps in languages like grep, sed and awk to deal with data in a streaming fashion with a minimum of forked processes.

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

#18
post #17
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.

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…

Then you have the age old problem: what if one of the tasks in your pipeline(s) fails?
Post reply on HN