Live data from Hacker News

Ask HN: How can I get better at bash?

news.ycombinator.com

131–140 of 195 posts

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

#132

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.

I use a variety of operating systems, many of which put things in different locations. Hell, even across linuxes, locations differ.

Don't do this. $PATH exists for a reason.

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

#133
post #129
post #101

As silly as it sounds, when I was a new Unix SysAdmin, I read the entirety of "man 1 bash", which includes all bash builtins. I found that it improved by bash-foo 100x simply by knowing about so many of the utilities. I also took some cliff notes for things that seemed generally useful. I did it for an hour or so a night for a week or so. That being said, a few of my personal favorites to memorize: * Parameter expans…

Also CTRL-_ which is "undo whatever you just typed"

That is a great one I didn't know (and I've been a 'nix monkey professionally since 2005). Thanks!

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

#134

Earlier quoted context omitted.

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…

Bash is unbeatable as a functional concept for chaining command-line tools together. Once you start getting functions or even a lot of if/while constructs, it's usually time to switch to Python/Perl.

I wrote a guide to the shell (posted elsewhere here). I concur with this statement wholeheartedly. Bash as a command language is exceptional, as a scripting language it is sub-par at best. I've been using Ruby a lot lately in command line scripting, little things like

$ ls /*.orig | ruby -e 'while f = gets do ... end'

It's not quite as easy as the shell tools for little things, but I feel like the crossover point where the lack of programming constructs begins to outweigh the initial ease of Bash scripting is about ten lines of Bash. Which is not to say that it's not useful -- I do think that Bash is something that every developer should know -- but that you really need to have Bash and another command line scripting language in your toolbox, and know when to use each.

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

#135

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

I can second the shellcheck recommendation. Shellcheck makes great recommendations, and every suggestion has a corresponding code you can google to get to a detailed explanation of why that is considered a warning or error. Hell, even if I just considered the times I forgot to put quotes around a variable and got warned by shellcheck I would be happy that I use it.

http://www.shellcheck.net/

https://packagecontrol.io/packages/SublimeLinter-shellcheck

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

#136
post #22

Earlier quoted context omitted.

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.

Agree. Perl is terrific for quick text processing. You can make surprisingly powerful one-liners.

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

#138

I have written a simple tool called mann ( https://github.com/soheilpro/mann ) to help me remember little things that I learn when working in Bash/Zsh. Basically, every time I learn something useful about a command, I add it to its mann page and then whenever I need it in the future, I simply run 'mann ' to find it. Here's the current output of my 'mann sed', for example: # Add char to beginning of each line sed 's/^…

This looks great. I've always kept those types of little snippets in the README of my dotfiles repo - and always keep a printed out copy on my desk. But this seems way more practical.

https://github.com/ben174/dotfiles/blob/master/README.md

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

#139
post #83

Don't listen to the guys who are saying not to learn bash. In the right circumstance bash is much better than any verbose python script. I'd say learn the following topics: pipe grep sed awk find Once you feel comfortable using and combining these tools you should be able to find out the rest by yourself.

I prefer Perl over Awk.

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

#140
post #129
post #101

As silly as it sounds, when I was a new Unix SysAdmin, I read the entirety of "man 1 bash", which includes all bash builtins. I found that it improved by bash-foo 100x simply by knowing about so many of the utilities. I also took some cliff notes for things that seemed generally useful. I did it for an hour or so a night for a week or so. That being said, a few of my personal favorites to memorize: * Parameter expans…

Also CTRL-_ which is "undo whatever you just typed"

Ok, but where is the "redo" option?
Post reply on HN