Live data from Hacker News

Ask HN: How can I get better at bash?

news.ycombinator.com

71–80 of 195 posts

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

#71
post #55

Earlier quoted context omitted.

> Like iterating over the files in a directory, for example. If you think that's easy in Bash you have either have a funny definition of easy Maybe I'm overlooking something...but why wouldn't this work: for file in $(ls); do { }; done

$ mkdir "hi there" $ mkdir "how are you" $ ls -l total 8 drwxr-xr-x 2 xxxxxxxx xxxx 4096 Jun 26 14:54 hi there drwxr-xr-x 2 xxxxxxxx xxxx 4096 Jun 26 14:54 how are you $ for f in $(ls); do echo $f; done hi there how are you

ls | while read file; do echo "$file"; done

... works better for the easy edge cases, but still probably has some issues. Personally I think klodolph called it; once you get into anything that has a few interesting edge cases bash becomes pretty unwieldy.

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

#72
post #59
post #16

Earlier quoted context omitted.

I would also recommend to read Single Unix Specification on shell syntax, and then avoid bashisms whenever possible. Bash had a history of subtly changing its behaviour in these, which leads to scripts getting suddenly broken without any modification on system update.

Could you give some examples? http://wiki.bash-hackers.org/scripting/bashchanges and http://wiki.bash-hackers.org/scripting/obsolete aren't particularly enlightening.

What I was hit with were subtle changes to how [[ ]] worked, along the lines of implicit anchoring in pattern in previous version not taking place in succeeding version. It took quite a while to debug what became a logic error.

After that I started avoiding bashisms in my scripts, especially that they usually provide very little benefit at the cost of gambling against breaking silently on a barely related update.

Nowadays it's also worth noting that #!/bin/sh often (Debian derivatives) points to something that is not Bash, and people writing in shell/Bash usually don't understand the difference.

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

#73
post #55

Earlier quoted context omitted.

In both my personal and professional life, I have the opposite conclusion. Bash is only for the simplest scripts and pipelines, and everything else (assuming you're going to use it more than once) gets written in Python or Go. The Bash syntax is not daunting, or if it is that's never been the problem with Bash. The problem is that Bash or shell programming in general gives you a million ways to shoot yourself in the…

> Like iterating over the files in a directory, for example. If you think that's easy in Bash you have either have a funny definition of easy Maybe I'm overlooking something...but why wouldn't this work: for file in $(ls); do { }; done

[deleted]

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

#74
Learn to move around efficiently: End of Line, Beginning of Line, Move by word so you aren't just abusing your keyboard.

Knowing how to reverse search (Ctrl-R) and run last command !vim or !curl to rerun last instance of vim or curl command with args so you don't have to search every time.

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

#76
I was about to post:

> Add, "#! /usr/bin/python" to the top of your scripts, it will make your life easier.

However, after reading the rest of the thread, it seems Python and similar langs are not actually great for the kind of things people use Bash for, and Perl is the way to go!

Great, another language to learn...

edit, re: python:

fiatjaf suggested xon.sh:

"shell language and command prompt [..] based on Python, with additional syntax added that makes calling subprocess commands, manipulating the environment, and dealing with the file system easy.

http://xon.sh/tutorial

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

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

I still have to re-learn control structures almost everytime I write a new script, I don't try and memorize [[]] vs [] and all the weird ways equality can work, I just google each time and answers are always on top (once you know what you're looking for).

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

#78
post #74

Learn to move around efficiently: End of Line, Beginning of Line, Move by word so you aren't just abusing your keyboard. Knowing how to reverse search (Ctrl-R) and run last command !vim or !curl to rerun last instance of vim or curl command with args so you don't have to search every time.

[deleted]

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

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

and keep a set of text files for yourself where after learning to do it the right way you write it down. I find that especially with bash/command line things I don't have to solve the same problem often (which would help with remembering) but often enough to be annoying having to re-learn it all the time, so it really pays off to have some documentation to refer to. org-mode and vimwiki are quite good for that.

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

#80
Take a look at this tutorial [1]. It will teach you some shortcuts, a bit of shell expansion, and help you set sane defaults in bash. One that I'm particular fond is to set

  "\e[A": history-search-backward 
  "\e[B": history-search-forward 
in your ~/.inputrc. So, if you are typing a command which begins with "git", it will only search in history for commands that start with git (instead of returning all commands that may include the string 'git' like Ctrl+r). Having trouble trying to remember that option you passed to `git log`? Just type in `git log` and press the up arrow to find your last usages.

I think it is also helpful to learn Emacs or vim keybindings. I use Emacs keybindings in bash a lot (enabled by default). I have summarized the ones that I used more often in a previous comment [2].

[1]: https://www.ukuug.org/events/linux2003/papers/bash_tips/

[2]: https://news.ycombinator.com/item?id=13404262

Post reply on HN