Live data from Hacker News

More Things I Wish I’d Known About Bash

zwischenzugs.com

21–30 of 50 posts

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

#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 clobber one of your own files. Especially dangerous if root :)

This has been a historic source of software vulnerabilities (often with the PID used instead as the guessable component instead of random, though). One recommended alternative is to use the `mktemp` command instead.

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

#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 and scripting idioms from bash pay dividends.

For situations where I need better arrays, associative arrays, floating point arithmetic, I'm probably better off writing it in an actual scripting language.

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

#24
post #5

Sigh. Hit ‘up’, ‘left’ until at the ‘p’ and type ‘e’ and return. One could also use , and to achieve the same result much quicker.

True, I use that too, but gets trickier in the middle of the line.

As a for-your-consideration, C-r leaves the cursor at the matched string when doing reverse search; so "echo helol world", enter, mutter "rats", C-r, o-l-sp, right (just to break the search), and voila you are now positioned on the offending substring

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

#25

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…

People keep mentioning zsh to me in response to this article - I think I'll have to go and read up on it more.

I still stick with bash - but if you're willing to move to a "friendlier" shell - my recommendation would be fish. Better out-of-the-box box experience than zsh - but still has the issue that most servers will have bash as the default interactive shell for root etc.

https://fishshell.com/

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

#26

Earlier quoted context omitted.

Yup. The moment you need editing capabilities more sophisticated than those three keys, though, I recommend switching to vi input mode (set -o vi) if you are familiar with vim keybindings. Although tapping Esc is not as quick as Ctrl (or, my chosen alternative, Ctrl-[), you have an entire library of editing commands already at your beck and call. I find that using 'f' or 'F' and '.' more quickly triangulates the prob…

IMO you might as well run "fc" (if you have vim set as your editor) in that case, rather than changing mode.

I've grown fond of the new :term command in vim (inspired by neovim).

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

#27

Portuguese speakers have counted for a long time with this gem, simply the best Bash doc/cheat sheet I've ever seen on the web. This is probably my oldest bookmark still relevant after 15 years. It's in portuguese and I'm not sure if there's an official translation, yet it's easy enough to decipher if you know bash, and Google Translate will do a pretty decent job. I gift you "Aurelio's Swiss Army Knife of the Bash S…

Thanks for the heads up - it does work via translate.

I'll drop this: http://tldp.org/LDP/abs/html/ If anyone, who has to do anything BASH related, has not seen it then they should!

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

#28
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…

this is from a guy writing abook on Docker, so I hope this is for a single purpose cointainer with now other users of THAT /tmp

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

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

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

#30

"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.

I don't like to use arrow keys, and on my system the following works: CTRL-P CTRL-A CTRL-F CTRL-F e I also prefer this to the solution in the article.
Post reply on HN