Live data from Hacker News

Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

github.com

61–70 of 114 posts

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#65
post #10

Earlier quoted context omitted.

You can use: VAR=$(!!) to accomplish something similar. Not by default of course. It re-runs the command, so if not idempotent/etc it will not return expected results. Also when re-run, the command will not be in a tty context, so if the executable is sensitive to such things (e.g. `ls`), the output format might be different.

Yeah, I know, but I don’t want to re-run and I want it by default.

You could put that line in your PS1, I think?

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#66

Earlier quoted context omitted.

When I need to shuffle around and/or rename my media files, for instance, it's risky to operate on the originals themselves. I've screwed up hundreds of mp3 files by issuing a bad `rename` command. I've lost the hierarchical structures of genres and artists and albums and such by accidentally moving them into the same directory together. And so on. If you `lndir` your mp3 directory to a functional copy of it, a playg…

You could use hardlinks for this, though (as long as both places are in the same filesystem). Then when you're finishes you just delete the old folder.

Huh. As a hardlink avoider, I never even thought of this. (I don't have a good reason for avoiding hardlinks - it's mainly just because I didn't grok them enough to predict their behavior. When you teach yourself, you gotta expect some gaps!) Thanks for the suggestion.

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#67
post #18

> Ctrl + x + Ctrl + e : launch editor defined by $EDITOR to input your command. Useful for multi-line commands. -- Great, I was just trying to remember that key combination the other day. Just got back to work after being for awhile for a child bonding leave.

Is that Ctrl+x, Ctrl+e? Or Ctrl+x+e? Or both controls?

all these work for me

  - C-x, C-e (separately)
  - C-(x,e)  (hold ctrl, x and e separately)
  - C-(x+e)  (hold ctrl-x and press e)

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#68
post #43
post #13

sed -i Watch out, that's a Linux-ism and macOS's sed will cheerfully use the thing after it as the backup expression; as far as I know, the absolute safest choice is to always specify a backup extension "sed -i~" or "sed -i.bak" to make it portable, although there are plenty of work-arounds trying to detect which is which and "${SED_I} -e whatever" type silliness My contribution (and yeah, I know, PR it ...) is that…

Using raw escape codes is ugly and device-dependent. People learned this in the 1970’s, and created libraries to get away from having to hard-code escape codes. Here’s a device-independent variant: title(){ tput tsl || tput -T xterm+sl tsl; printf %s "$*"; tput fsl || tput -T xterm+sl fsl; } Note: if the terminal does not advertise support of the necessary capabilities, it falls back to using the XTerm escape sequenc…

Having arrived back at my Mac, running iTerm2, I wanted to share another fun fact about using those executables: running that function while the outer shell is in "set -x" causes the title of the window to be

    + printf %s 'hello world'^M^Jhello world+ tput fsl^M^J+tput -T xterm
and that's where it cuts off but I presume ends with "^M^J" at the end :-D

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#69
post #54
post #17

Earlier quoted context omitted.

I had been in the habit of symlinking ~/.bash_history to /dev/null to avoid AFS/NFS writes on every local command execution. When I moved over to the financial industry, it didn't occur to me that such a symlink might look like an attempt to evade monitoring. A year or two in, I realized it didn't look good, but it had clearly been made my first week on the job, so I just left it in place for over 10 years rather tha…

> I hope and presume they had much better monitoring than scanning bash history, but I'm not bet-my-career confident of that. bash has an "audit" function which is normally compiled out. https://git.savannah.gnu.org/cgit/bash.git/tree/configure#n1... When enabled it logs to syslog.

Enterprises that requires logging of user actions will very likely not being doing it at the shell level, either through compiled in options, or shell history.

Instead, the Kernel has built in functionality called Auditd[0], which is capable of logging any and all executions, file or socket accesses, and much more. Along with included tooling for quickly finding and alerting on events[3].

Further, if terminal logging or playback is really required (usually not), it's generally done through pam with tlog[1]. Red Hat 8 and above come with built-in tlog support[2].

[0] https://access.redhat.com/documentation/en-us/red_hat_enterp...

[1] https://github.com/Scribery/tlog/blob/main/README.md

[2] https://access.redhat.com/documentation/en-us/red_hat_enterp...

[3] https://wiki.archlinux.org/title/Audit_framework

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#70

Something I’ve been wanting to do but haven’t found a perfect solution for yet: storing the output of previous command in some variable by default. I use Terminal.app’s Select Between Marks or tmux, but I wish this was a thing.

might be kludgey but i wonder if bash coproc would help, it basically can send stdout to a named pipe that makes it avail as a environment variable fike descriptor.
Post reply on HN