Live data from Hacker News

More Things I Wish I’d Known About Bash

zwischenzugs.com

31–40 of 50 posts

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

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

Nitpick: $RANDOM gives you an integer between 0 and 32767 (inclusive).

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

#33

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.

Or, if you already started editing a line, and decided that you'd rather finish it in a proper editor, you can press Ctrl+X Ctrl+E.

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

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

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

#35
post #33

Earlier quoted context omitted.

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

Or, if you already started editing a line, and decided that you'd rather finish it in a proper editor, you can press Ctrl+X Ctrl+E.

Didn't know that one, thanks.

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

#37

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…

Aurelio is the madman/genius behind Arkanoid in sed[1], Sokoban in sed[2] and sedsed, a sed debugger[3]. I have no trouble believing that he has a few shell tricks up his sleeve.

[1]: http://aurelio.net/projects/sedarkanoid/

[2]: http://aurelio.net/projects/sedsokoban/

[3]: http://aurelio.net/projects/sedsed/

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

#39
post #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!

I'd recommend against the Advanced Bash-Scripting Guide -- it uses a lot of bad practices (like unquoted variable references!) that you're better off not learning. Instead, check out http://mywiki.wooledge.org/BashGuide and http://mywiki.wooledge.org/BashFAQ. Also, http://www.shellcheck.net is a good automated tool for sanity-checking syntax errors and common mistakes.

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

#40
post #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.

Potentially of interest: https://github.com/Fakerr/goto sort of like the easymotion vim plugin.
Post reply on HN