Live data from Hacker News

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

github.com

11–20 of 114 posts

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

#11
post #10

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.

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.

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

#12
Not one-liners, but some of the tools that I have found helpful for working with large bash codebases are:

  - shellcheck and shfmt for linting and formatting
  - sub for organizing subcommands
  - bashdb for debugging scripts interactively via the VSCode extension
I'm still missing a way to share modules with others like it can be done with ansible/terraform, but I have not found an optimal way to do it yet.

[shellcheck] https://github.com/koalaman/shellcheck [shfmt] https://github.com/mvdan/sh [sub] https://github.com/qrush/sub [bashdb] http://bashdb.sourceforge.net/ [vscode-bash-debug] https://github.com/rogalmic/vscode-bash-debug

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

#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 I get a lot of mileage out of setting the terminal title from my scripts:

    title() { printf '\033]0;%s\007' "$*"; }
and there's one for tmux, too

    printf '\033]2;%s\007' "$*";
with the two infinitely handy resources:

* https://www.xfree86.org/current/ctlseqs.html

* https://iterm2.com/documentation-escape-codes.html

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

#14

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.

Perhaps you could change your prompt so that everything is wrapped with tee.

Interesting rabbit hole to go down…

https://unix.stackexchange.com/q/562018

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

#15
post #3

Learnt a neat trick from an old sysadmin colleague. If you’ve written a command but realize you don’t want to run it right now but want to save it in your history you can just put a `#` in front of it (ctrl-a #) making it a comment and allowing you to save it in your history without running it. When you’re ready to run it you find it and remove the preceding `#`

You can achieve the same thing by typing a command, then hitting Escape followed by '#'.

It will prefix your current commandline with a '#' and "run" it.

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

#16
I love these one-liners. It's also about knowing your tools better.

I hadn't known about `look` [0], which is great.

The writer looks to be a bioinformatician, so it might be a bit out of scope, but I also found `socat` [1] quite a good serial communication helper tool.

[0] https://man7.org/linux/man-pages/man1/look.1.html

[1] https://linux.die.net/man/1/socat

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

#17
post #4
post #3

Learnt a neat trick from an old sysadmin colleague. If you’ve written a command but realize you don’t want to run it right now but want to save it in your history you can just put a `#` in front of it (ctrl-a #) making it a comment and allowing you to save it in your history without running it. When you’re ready to run it you find it and remove the preceding `#`

The opposite is adding space before the command. The command will run but it will not be saved in history. EDIT: This apparently needs to be configured - setting HISTCONTROL=ignorespace

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 than risk looking like I was again monkeying with my history.

I hope and presume they had much better monitoring than scanning bash history, but I'm not bet-my-career confident of that.

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

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

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

#19
post #3

Learnt a neat trick from an old sysadmin colleague. If you’ve written a command but realize you don’t want to run it right now but want to save it in your history you can just put a `#` in front of it (ctrl-a #) making it a comment and allowing you to save it in your history without running it. When you’re ready to run it you find it and remove the preceding `#`

I wonder if it isn't possible to get it to save your command to history when you do Ctrl-C.

I tried a naive way by trapping sigint but couldn't get it to work.

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

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

Yeah please do open PRs for these!
Post reply on HN