Live data from Hacker News

Things I Wish I'd Known About Bash

zwischenzugs.com

1–10 of 272 posts

Re: Things I Wish I'd Known About Bash

#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 locally and executing it via SSH)

Re: Things I Wish I'd Known About Bash

#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)" = '' ]

Re: Things I Wish I'd Known About Bash

#6
The sections on quoting and globbing suggest this author, though obviously trying to be helpful, isn't really knowledgeable enough to be writing such a guide.

I suggest reading this instead: http://www.grymoire.com/Unix/Sh.html

Granted, it's about the Bourne shell, but since Bash, Korn, and every standard UNIX shell is supposed to be compatible with it, it's well worth learning.

And IMO, if you need more than what the Bourne shell provides, you should be using a proper programming language like Python instead.

Re: Things I Wish I'd Known About Bash

#8

    rename -n 's/(.*)/new$1$2/' *
There's a chance this won't work on your system.

There are two incompatible versions of rename in the wild:

1) Perl one: https://metacpan.org/pod/distribution/File-Rename/rename.PL

2) from util linux: http://man7.org/linux/man-pages/man1/rename.1.html

Debian (and dertivaties) ship the former; other Linux distros likely the latter.

Re: Things I Wish I'd Known About Bash

#10

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

Can you explain why?

For portability. bash is not available everywhere, but POSIX sh is a requirement of POSIX so you can expect it to be there.
Post reply on HN