Live data from Hacker News

Things I Wish I'd Known About Bash

zwischenzugs.com

111–120 of 272 posts

Re: Things I Wish I'd Known About Bash

#111
Interesting but from a modern perspective is not perl a better scripting language to learn.

I have never used bash scripts professionally i.e. as a language rather than a simple script with just a command in.

Like wise back in 87 or so I got trained in sed and I have only used it once since, and that was when I was playing around with early linix's (when it came on a huge number of floppys)

Re: Things I Wish I'd Known About Bash

#112
post #84

Earlier quoted context omitted.

If foo has a Texinfo manual (GNU tools like bash usually do) then you can try `info foo` and search the index with i or I for -p. Texinfo manuals also have hyperlinks you can press enter on. info is a greatly underused system and I'd recommend any *nix users to spend some time learning how to navigate it.

Thanks. Is there a good way to open that in a browser, rather than a console?

In general, the best way to read Info documentation is inside Emacs.

Re: Things I Wish I'd Known About Bash

#113

Earlier quoted context omitted.

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?

Yes, you would write non-trivial install scripts in ruby. https://en.m.wikipedia.org/wiki/Chef_(software)

If any of my bash scripts get long enough to where I want a reusable class, I rewrite in Ruby.

Re: Things I Wish I'd Known About Bash

#114

Interesting but from a modern perspective is not perl a better scripting language to learn. I have never used bash scripts professionally i.e. as a language rather than a simple script with just a command in. Like wise back in 87 or so I got trained in sed and I have only used it once since, and that was when I was playing around with early linix's (when it came on a huge number of floppys)

You have a terminal if you're on a Mac or Linux machine. It takes bash commands by default. Knowing perl is good, but knowing more bash is always good.

I hadn't known that <(echo "hi") is treated as a file with the contents of stdout, which simplifies commands that take files as arguments.

Re: Things I Wish I'd Known About Bash

#115
post #105
post #3

> if [ x$(grep not_there /dev/null) = 'x' ] This is still wrong if the command can output spaces or meta-characters. You should quote the left operand, and then you don't need to prepend x: if [ "$(grep not_there /dev/null)" = '' ]

Does the second only work with bash? Because IIRC it didn't work with FreeBSD's /bin/sh, you needed the initial x too.

No, this should work in any POSIX-compliant shell.

Re: Things I Wish I'd Known About Bash

#116

    $ grep somestring file1 > /tmp/a
    $ grep somestring file2 > /tmp/b
    $ diff /tmp/a /tmp/b
You shouldn't do that, but not because it's not neat enough. /tmp is world-writable, so you might be writing to somebody else's file, or over a symlink that was set up by someone else. Use mktemp¹ for creating temporary files.

¹ http://man7.org/linux/man-pages/man1/mktemp.1.html

Re: Things I Wish I'd Known About Bash

#118
If you use a thing a lot, you should probably invest some time in reading the manual for it.

The manual for bash consists of its Unix-style "man" page, and is therefore more of a reference than an instruction manual. I suggest using the Advanced Bash-Scripting Guide (http://tldp.org/guides.html#abs).

Re: Things I Wish I'd Known About Bash

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

Bash has too many surprising edge cases. A lot of my install scripts have at least snippets of Perl in them. The installation/upgrade/maintenance scripts for my employer's main product is Ruby-based.

We support a lot of different OSes, and there's usually less variance between the Perl deployed on them than there is in the shell.

Re: Things I Wish I'd Known About Bash

#120
post #2

My take on the same topic: - use the unofficial strict mode: http://redsymbol.net/articles/unofficial-bash-strict-mode/ - use parameter substitutions like ${foo#prefix}, ${foo%suffix} instead of invoking sed/awk - process substitution instead of named pipes: () - know the difference between an inline group {} and a subshell () - use printf "%q" when passing variables to another shell (e.g. assembling a command locall…

While process substitution is great on the surface, it comes with a troubling downside: There is no way, that I have found, to reliably catch errors. That is, this:

  some-command 
...will succeed. And -e and pipefail do nothing here. I have not found any way to push the error up. Plenty of questions on Stackoverflow and elsewhere, no good answers.
Post reply on HN