Live data from Hacker News

Ask HN: How can I get better at bash?

news.ycombinator.com

111–120 of 195 posts

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

#111

Earlier quoted context omitted.

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?

I'm sure there are other reasons, but the big one is that your scripts may get called in an environment other than your normal logged in shell.

Cron, for example, doesn't have the same $PATH as your login shell. So no full paths means you can fail to run some commands, or run the wrong copy of one.

There's also the security aspect. If your script has "." in it's $PATH, or something else writeable, I may be able to coerce it to run an imposter command.

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

#112
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…

+1 for the shortcuts.

Also one should take a look at rlwrap after becoming comfortable with the keyboard shortcuts.

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

#113
Besides the obvious answers of just reading the manual, looking up howtos, and stackoverflow I can recommend some habits that might increase your uptake of bash.

1. If you are not running a unix as your default OS switch to one (ie Linux or Mac).

2. Create a bin (~/bin) directory in your home directory of all your shells scripts and source control it. Any script you ever write put in that directory. Even if its not bash (ie python, perl). I find that it is critical to look at how you did things previously to help you learn as well as it saves time.

3. Any command that is complicated one liner that you create or see on the internet... create script and put in the bin directory mentioned above.

4. Optimize your personal bin directory and review frequently.

5. If you run Linux build your system from scratch (ie read Linux from scratch).

6. Bonus to the above: Automate the creation of your personal system through Packer and Bash!

7. Find where things are not automated.

8. Bash is more than just the shell. A knowledge of GNU coreutils as well as tmux/screen is worthwhile and highly recommended.

9. Learn the readline shortcuts. Particularly "ctrl-r".

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

#114
I found this useful for getting deeper with using bash: "Pro Bash Programming : Scripting the GNU/Linux Shell, Second Edition." It is kind of a brain dump type of book but it called out a bunch of little things I had missed in looking at other information sources.

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

#116
GNU BASH is a branch of the Bourne-shell family.

Korn shell is a much more complete and capable variant of Bourne. BASH partially implemented many Korn features, but not everything.

The standard reference is the Korn and Bolsky book (2nd edition). I'm not aware of any free/online resources that are profoundly good.

Korn is the very best for scripting.

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

#118

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…

> if your bash script grows beyond about ten lines, it's time to consider rewriting it in a cleaner language

Just because you don't feel comfortable writing long scripts doesn't mean you should discourage others.

There are many many justifications for sticking to shell, for example if you need to write a portable installer that works across every UNIX variation.

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

#119
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…

Yes, the man page is very good. The online gnu documentation is good, too. Some of the information will not be categorized as bash.[1] Also check out sed and awk.

[1] http://www.gnu.org/software/coreutils/manual/html_node/index...

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

#120

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…

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.
Post reply on HN