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.
Bash-oneliner: A collection of handy Bash one-liners and terminal tricks
11–20 of 114 posts
Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks
#12 - 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 sillinessMy 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:Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks
#14Something 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.
Interesting rabbit hole to go down…
Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks
#15Learnt 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 `#`
It will prefix your current commandline with a '#' and "run" it.
Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks
#16I 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.
Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks
#17Learnt 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 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
#18Great, 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
#19Learnt 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 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
#20sed -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…