Live data from Hacker News

Ask HN: How can I get better at bash?

news.ycombinator.com

101–110 of 195 posts

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

#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 expansion: https://www.gnu.org/software/bash/manual/html_node/Shell-Par...

* All of test(1) as you can use them in any if statement (/usr/bin/[ is a real command!): https://linux.die.net/man/1/test

* Knowing most of the bash internal variables: http://tldp.org/LDP/abs/html/internalvariables.html

* Keyboard shortcuts and how they are useful. A few example: CTRL-l (no need to ever use /usr/bin/clear), CTRL-k, CTRL-u, CTRL-e, CTRL-a, CTRL-w, CTRL-arrow left, CTRL-arrow right, CTRL-r (history reverse search with find as you type autocomplete)

The best way you can learn the shell is by using Linux as your primary desktop for at least a few months. You'll get very proficient very quickly by doing that.

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

#104
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/^/#/'

  # Replace with newline
  sed 's//\'$'\n''/g'

  # Replace newline
  sed -e ':a' -e 'N' -e '$!ba' -e 's/\n//g'

  # Plus sign
  sed -E 's/foo+/bar'

  # Digit
  sed -E 's/[[:digit:]]/bar'

  # Inplace
  sed -i'.bak' -e  

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

#107
I recommend also learning a different shell's quirks and syntax. I started using fish a couple years ago, but I still have to write any 'production' scripts in bash. Learning fish cleared up lots of misunderstandings I had with bash and has made my bash much improved.

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

#108

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've heard this suggestion to use full paths for a long time. Why use full paths?

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

#109

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

Google Whack!

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

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

At the risk of being pedantic, grep sed awk and find are not bash, they are their own programs. You can run find from any shell, or without a shell at all.
Post reply on HN