I was surprised to see `$()` missing from this (otherwise quite extensive) list. There are a few commands listed which employ it, but it absolutely deserves its own entry. That and `readlink -f` to get the absolute path of a file. (Doesn't work on MacOS; the only substitute I've found is to install `greadlink`.) And `cp -a`, which is like `cp -r`, but it leaves permissions intact - meaning that you can prepend `sudo`…
Bash-oneliner: A collection of handy Bash one-liners and terminal tricks
51–60 of 114 posts
Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks
#52Something 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
#53Earlier quoted context omitted.
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…
As a point of comparison, I ssh-ed into my mac and ran "tput -T xterm+sl tsl" in order to see what it would output, and it hung my connection So, I'll stick to my printf thanks
If you want to see what bytes it would output, use “od”:
tput -T xterm+sl tsl | od -t cRe: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks
#54Earlier quoted context omitted.
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 tha…
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.
Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks
#55> 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
#56I was surprised to see `$()` missing from this (otherwise quite extensive) list. There are a few commands listed which employ it, but it absolutely deserves its own entry. That and `readlink -f` to get the absolute path of a file. (Doesn't work on MacOS; the only substitute I've found is to install `greadlink`.) And `cp -a`, which is like `cp -r`, but it leaves permissions intact - meaning that you can prepend `sudo`…
Other languages have this also. In fact it's a C language system call in Linux, though it only goes one indirection level at a time.
This is useful if you run a script and want to use files contained in the directory where the script is located. This comes up if you have a symlink to the script in any /something/something-else/bin directory.
Use cases:
Script needs its own location for e.g. configuration files.
User of the script needs the location for e.g. another script that needs to be run that isn't in the search path.
Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks
#57Earlier quoted context omitted.
As a point of comparison, I ssh-ed into my mac and ran "tput -T xterm+sl tsl" in order to see what it would output, and it hung my connection So, I'll stick to my printf thanks
You can’t run only one of the tput commands! You need to run both of them, as in the shell function; i.e. both tsl and fsl needs to be sent to the terminal! If you want to see what bytes it would output, use “od”: tput -T xterm+sl tsl | od -t c
As a concrete example, my printf version works even when run inside docker, but
$ docker run --rm ubuntu:22.04 bash -c '{ tput tsl || tput -T xterm+sl tsl; } | od -c'
tput: No value for $TERM and no -T specified
tput: unknown terminal "xterm+sl"
0000000Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks
#58Whenever you need to use a single-quote on command line add a $ sign before it. It makes escaping everything super easy
su user -c $'cd \'$dir\' && ...'
Before this it used to confuse the hell out of me.More details are here: https://stackoverflow.com/a/16605140/1031454
Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks
#59Earlier quoted context omitted.
>a copy of your original directory, with only the modified files actually taking up space What's the use case for this sort of thing?
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…
Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks
#60I was surprised to see `$()` missing from this (otherwise quite extensive) list. There are a few commands listed which employ it, but it absolutely deserves its own entry. That and `readlink -f` to get the absolute path of a file. (Doesn't work on MacOS; the only substitute I've found is to install `greadlink`.) And `cp -a`, which is like `cp -r`, but it leaves permissions intact - meaning that you can prepend `sudo`…
Note realpath is the preferred interface these days. For some history see: https://unix.stackexchange.com/a/136527
I was surprised to see it was already installed with coreutils, so I've apparently had it for a while now. Thanks for the heads up.