Ask HN: How can I get better at bash?
131–140 of 195 posts
Re: Ask HN: How can I get better at bash?
#132Most 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.
Don't do this. $PATH exists for a reason.
Re: Ask HN: How can I get better at bash?
#133As 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"
Re: Ask HN: How can I get better at bash?
#134Earlier 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.
$ 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?
#135Read 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.
Re: Ask HN: How can I get better at bash?
#136Earlier 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.
Re: Ask HN: How can I get better at bash?
#137https://web.stanford.edu/class/cs124/kwc-unix-for-poets.pdf
(text analysis in bash)
Re: Ask HN: How can I get better at bash?
#138I 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/^…
Re: Ask HN: How can I get better at bash?
#139Don'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.
Re: Ask HN: How can I get better at bash?
#140As 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"