Live data from Hacker News

Things I Wish I'd Known About Bash

zwischenzugs.com

41–50 of 272 posts

Re: Things I Wish I'd Known About Bash

#41
post #15

Can someone explain :h as the article's description was not clear at all to me what was going on there.

Yeah, I couldn't make that work for me (on Linux or MacOS) .. although I'd love it if there were a way to quickly get 'just the directory' or 'just the filename' in bash with a shortcut, instead of having to resort to $(dirname blah) and so on .. I'm sure there is some way but :h doesn't look to be the shortcut as expected.

    $ echo foo bar
    foo bar

    $ echo !$
    echo bar
    bar
The bang syntax is for history expansion, so it won't work on directories directly. But you could hack that with something like:

    $ls /root/my-dir
    [...]

    $echo !:t
    echo my-dir
    my-dir

Re: Things I Wish I'd Known About Bash

#42
post #20

Bash has a huge number of little shortcuts that are difficult to learn. When one encounters a sequence of symbols like $(...), it is difficult to Google for its meaning. The reason shells nevertheless have these shortcuts is of course because they are shells: from the commandline it can be very convenient to use shortcuts. But, in my opinion, that's where it should stop: one shouldn't use a shell language for scripti…

This is why the less program has the / command — so you can search the man page for the syntax you are curious about.

Re: Things I Wish I'd Known About Bash

#43
If you want all the args to the previous (or earlier) command just use !. (e.g. a common idiom for me is cat `!`). Or you tried a git mv out of habit but the dir isn’t being managed by git: !gi:

Re: Things I Wish I'd Known About Bash

#45
post #30

I wrote a book on Bash too. The most important thing for anyone to know about Bash is that it's intended as a command language, not a general purpose scripting language. If it's longer than 10 lines, or if it uses two or more variables, you should probably have written it in something other than Bash.

Would you consider non-trivial install scripts as an exception to this general rule? I mean, you wouldn't write some install script in ruby or python, right?

Re: Things I Wish I'd Known About Bash

#46

#0: If you're writing scripts that are destined for other users, use POSIX sh instead.

Do you know of any learning resources that are strictly POSIX sh instead of a specific shell? I'm looking to learn and it will be very helpful.

How about the standard?

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3...

Re: Things I Wish I'd Known About Bash

#47

+1 for the parts that are portable to Bourne shell/ksh/zsh. -1000 for the parts that are specific to Bash. Stuff like that has been huge pain in my ass over the years. Some clever programmer uses some bash-ism and the build breaks on some ancient hardware that doesn't have bash. I realize my complaint sounds like Henry Spencer's Ten Commandments and perhaps it feels outdated but trust me, you don't want to wade into…

>Stuff like that has been huge pain in my ass over the years. Some clever programmer uses some bash-ism and the build breaks on some ancient hardware that doesn't have bash.

Shouldn't the problem be the "ancient hardware that doesn't have bash" itself?

Re: Things I Wish I'd Known About Bash

#48
post #24

Earlier quoted context omitted.

Man pages are specifically reference documents, not tutorials or guidebooks. To say one should use a man page is to say that one must completely digest the entirety of the tool prior to ever actually using it. That’s just simply not feasible, nor should it be expected of anyone beyond trivial tools. Man pages simply don’t provide the context for solving a problem like a guidebook or tutorial would, which is why there…

Which is why the parent advised to treat the man page like a reference document, by searching in it. Some man pages are just badly written and are indigestible even when searching for a specific thing, but in general, that approach works quite often.

Is there some trick to searching man pages that I don’t know? Because my usual experience is:

  type man foo
  type /-p
  type n n n n n n n n n
as there are a bunch of matches like “...does bar when combined with -p...”

A presentation of man pages that used hypertext would make me a lot happier.

Re: Things I Wish I'd Known About Bash

#49
Two important things missing:

1) This is a huge pet peeve of mine, but it kills me when my coworkers "bash" emacs and sing the praise of vim, then proceed to explain to me that ctrl-R in bash searches for the last command with a given pattern. They also often refuse to believe me that they're basically using emacs controls (because you know emacs' dirty). So, please, if you're in love with vim and use ksh or bash, learn about "set -o vi". Oh, and stop preaching!

2) "help xxx" for any xxx bash functionality, right there from the command line!

Re: Things I Wish I'd Known About Bash

#50
shellcheck (https://www.shellcheck.net) is an absolute godsend when writing Bash/POSIX sh scripts.

It catches so many errors that I think it's a must have in every programmer's toolbox. It even catches bash-isms when you are targeting POSIX sh. It saved me many many hours of grief trying to debug shell scripts I wrote and changed the way I write them for the better.

Post reply on HN