Live data from Hacker News

Ask HN: How can I get better at bash?

news.ycombinator.com

91–100 of 195 posts

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

#91

Most of the responses here so far that do not include some sort of a guide are not the responses you're looking for (imho). Mind your pipes and quotes. Guard your variables with braces. Do not export everything, test for and (try to) handle return codes and conditions and keep it simple (emphasis simple) but most of all just write it. BASH (or Bourne) is ubiquitous when dealing with systems (vs programs). You don't n…

1000 times this.

You're going to get a lot of snark from people saying things like "don't", or "learn python instead".

This epitomizes "a little knowledge is a dangerous thing".

Bash has many cringeworthy aspects, but we have to deal with the world as it is, not the world as we would like it to be, and the reality is that bash is the default shell on 99.9% of unix boxes you encounter — even if you use an alt shell on your machine.

Coworkers machine? Bash. Default AWS AMI? Bash. init script to bootstrap $DAEMON? Bash. ssh to a server at your workplace? Bash. Random O'Reilly Linux tutorial? Assumes bash.

My advice?

Take some time.

Sit down.

and read "man bash"

cover-to-cover.

at least once.

A lot of the illogical things in bash make a lot more sense once you understand its parsing/expansion rules. And once you understand what is in the language vs an external terminal program in your PATH.

Since that sounds unappealing (and I scoffed at that very advice for many years), I've also found the wooledge bash guide to be very helpful.

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

#93
First, if your bash script grows beyond about ten lines, it's time to consider rewriting it in a cleaner language. Python's a common one. I used to use Haskell for that kind of scripting as well, which was astonishingly good at it.

Here's my study suggestion:

0. Learn to use variable interpolation and backticks.

1. if blocks and the [ built-in function. Go read about the grammar and look at the flags that [ takes. Memorize the most common couple (file exists, is a directory), and know how to look up the others when needed. Find examples of variable interpolation tricks needed to make this function.

2. for and while blocks. Learn the grammer. for is mostly useful with `seq ...` or a file glob.

3. Learn some of the options to make bash fail early and loudly like pipefail.

4. Most of the power of bash is in the programs you call, and they aren't always the same ones you use interactively. Other folks have mentioned some of these. find, xargs, wait...

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

#94
post #77
post #62

Train yourself to take the 20 minutes required to learn to "do it the right way" every time you need to. Its so easy not to bother because you are busy but in the long run you will save time.

I think this is the right answer, assuming OP actually wants to get better at bash and not route around his original question by learning stuff that isn't bash. I use bash/shell scripts frequently and have many running 'in production' as cron jobs th at run various jobs or manipulate data for other jobs to run on. One thing I really like about pure shell is that it's extremely portable and transparent about what it's…

I couldn't begin to say how many times I've typed "help test". It's a lot. I usually get the answer I'm looking for, when it comes to conditionals.

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

#95

First, if your bash script grows beyond about ten lines, it's time to consider rewriting it in a cleaner language. Python's a common one. I used to use Haskell for that kind of scripting as well, which was astonishingly good at it. Here's my study suggestion: 0. Learn to use variable interpolation and backticks. 1. if blocks and the [ built-in function. Go read about the grammar and look at the flags that [ takes. Me…

I agree that writing long, monolithic scripts isn't optimal, but there's no reason you can't break a large script into multiple smaller scripts. And there are certain tasks where won't offer any advantage over using a regular ol' shell script. If one really needs/wants to get into Bash, I'd recommend learning it well enough to make educated decisions about when Bash starts becoming a pain, and the benefits of start to kick in. In some cases, a will be more of a hinderance than a help, especially in areas where the the functionality you want cannot be found in the standard library.

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

#96

Most of the responses here so far that do not include some sort of a guide are not the responses you're looking for (imho). Mind your pipes and quotes. Guard your variables with braces. Do not export everything, test for and (try to) handle return codes and conditions and keep it simple (emphasis simple) but most of all just write it. BASH (or Bourne) is ubiquitous when dealing with systems (vs programs). You don't n…

https://www.google.com/search?q=whizbangwoohoo

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

#97
The best I can really recommend is practice. A lot of "bash" skill comes not from bash itself, but rather from the tools around it.

If you're not already comfortable with input/output redirection (including pipes, as well as reading from / writing to files via file, respectively), then that's where I'd start.

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

#98

Most of the responses here so far that do not include some sort of a guide are not the responses you're looking for (imho). Mind your pipes and quotes. Guard your variables with braces. Do not export everything, test for and (try to) handle return codes and conditions and keep it simple (emphasis simple) but most of all just write it. BASH (or Bourne) is ubiquitous when dealing with systems (vs programs). You don't n…

I've been using linux since 1992 and if there's one thing I can't stress enough is to use full directories and not anything abbreviated. After 25 years, I still find myself slipping up, overlooking some minute detail, causing data loss.

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

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

Tcl lacks the piping syntax by default, but there are various ways to implement it: http://wiki.tcl.tk/17419
Post reply on HN