Live data from Hacker News

More Things I Wish I’d Known About Bash

zwischenzugs.com

41–50 of 50 posts

Re: More Things I Wish I’d Known About Bash

#41

But you really should know that set -e is broken.

So... why the downvotes? Is it not the case that set -e is broken? Or is it that I provided too little detail? Or is it that it's broken in the same way in all shells?

FWIW, the bug (yes, it's a bug that the Open Group has decided is not a bug, but it really cannot be anything other than a bug!) is this: non-zero exits by commands/functions are ignored when invoked by functions invoked in a conditional expression context.

Re: More Things I Wish I’d Known About Bash

#42
post #21

This is a somewhat dangerous pattern for picking temporary files (from #8): $ NEWFILE=/tmp/newfile_${RANDOM} $ touch $NEWFILE The problem is that any user on the box can create files under /tmp. An attacker can set up a bunch of symlinks like /tmp/newfile_1, ..., /tmp/newfile_99999 pointing to a file owned by your user. When your script then writes into this temporary file, you'll write through the symlink and clobbe…

it's a very dangerous pattern. Use mktemp(1) instead!

Re: More Things I Wish I’d Known About Bash

#43

I imagine most uses of random in Bash don't need to be that robust, but it might be worth mentioning that ${RANDOM}${RANDOM} has a lot of bias as a random number generator.

If you want random numbers in bash then use dd if=/dev/urandom ...

$RANDOM is not cryptographically secure.

Re: More Things I Wish I’d Known About Bash

#44

"Sigh. Hit ‘up’, ‘left’ until at the ‘p’ and type ‘e’ and return.". My solution for this one would be : UP CTRL-A RIGHT RIGHT e Which needs less thinking and 6 keystrokes instead of 8.

But that requires modifiers. Insert modal vs. not flame war.

Re: More Things I Wish I’d Known About Bash

#45
I use pushd and popd every day. If you forget what's in the stack you can see what's in it by using the "dirs" command.

You can also use pushd -n (where n is a number, although I usually end up needing trial and error to get the right one) to rotate the list of dirs without removing any from the stack - useful if the list has more than 2 directories, or 2+ directories you need to switch between repeatedly.

pushd and popd also work out of the box in Windows command prompt, although you don't get "dirs" or any fancier options like in bash, just push and pop on a plain old stack.

Re: More Things I Wish I’d Known About Bash

#49
post #22

Just another plug for Zsh: it has all of these features and then some. - Safe-by-default parameter expansion: no word splitting unless you ask for it, even if you don't quote the expansion. - Ability to use histoy expansions (like !!:gs/foo/bar) on parameter expansions, meaning "${foo:A:h}" is equivalent to "$(dirname $(realpath $foo))" - Much better array support, including both integer-indexed and associative array…

Production servers don't generally have zsh available, while most have bash. That means that ssh sessions will use a different shell, and scripts I write will use a different shell. And so, the upside for using some funky shell locally is greatly reduced. Whereas if I stick with bash, I can run my scripts almost everywhere and almost every server I ssh into has a familiar environment. Thus my knowledge of edge cases…

Fair enough! One tends to get spoiled with Zsh, trying to go back to Bash.

In the past I've actually downloaded Zsh, compiled it from source, and ran it out of ~/.local/bin with absolutely no issues. But that's not something I'd advise.

Re: More Things I Wish I’d Known About Bash

#50
post #34

I agree with most of it except for: ${RANDOM}${RANDOM} A preferred way would be od -vAn -N4 -tu4 /dev/urandom gives you random bytes, od dumps them in different formats (e.g: hex, octal, decimal and such). This takes 4 random bytes and outputs them as a 4 byte unsigned int.

Explanation of options: -v: Don't suppress duplicate lines. (It doesn't make a difference here, because there's always only one line.) -An: Don't write input offsets. -N4: Read at most 4 bytes. -tu4: Print 4-byte unsigned integers.

https://explainshell.com/explain?cmd=od+-vAn+-N4+-tu4
Post reply on HN