Live data from Hacker News

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

github.com

31–40 of 114 posts

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

#31
post #29

Earlier quoted context omitted.

Even better, use a real text editor like ed or ex. (Nowadays ex is more portable because many distros — against POSIX — omit all 55 kilobytes of GNU ed. Of course, smaller systems might not have ex/vi.) Basic usage looks like this: printf '%s\n' '" some commands...' 'wq' | ex -s file Or: ex -s file By the way, these commands are the ones that you use in your vimrc or after a colon in vim — at least, the POSIX subset…

That's an interesting trick, I'll bear it in mind. That said, the "lottery factor" is often a bigger contributor to the things that land in codebases than "optimality". Plus, I've actually seen somewhere that perl is the most common binary across every system, and it's likely a larger population who know perl than ed would be my guess

Perl more common than vi? The study you’re remembering probably didn’t include POSIX utilities.

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

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

> Watch out, that's a Linux-ism and macOS's sed It's GNU sed vs (Free)BSD sed, which are different enhancements of the POSIX standards for sed that went in different design directions. One could Homebrew/macports install gnu-sed on macOS to get a GNU version to write Linux-portable scripts as-needed.

[deleted]

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

#33
post #29

Earlier quoted context omitted.

That's an interesting trick, I'll bear it in mind. That said, the "lottery factor" is often a bigger contributor to the things that land in codebases than "optimality". Plus, I've actually seen somewhere that perl is the most common binary across every system, and it's likely a larger population who know perl than ed would be my guess

Perl more common than vi? The study you’re remembering probably didn’t include POSIX utilities.

With the full understanding that "docker images" are their own special little things:

    $ docker run --rm ubuntu:22.04 bash -c 'command -v ex; command -v ed; command -v vi; command -v perl;' 
    /usr/bin/perl
and the same result for "debian:stable"

---

edit: I just realized that's because apt is _written in_ perl, but tomato, tomahto, and it may very well be that they picked perl for that same universal-binary reason

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

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

> Watch out, that's a Linux-ism and macOS's sed It's GNU sed vs (Free)BSD sed, which are different enhancements of the POSIX standards for sed that went in different design directions. One could Homebrew/macports install gnu-sed on macOS to get a GNU version to write Linux-portable scripts as-needed.

Plan9 sed has no -i option. Older versions of NetBSD will not have it either.

I never understood the point of the -i option other than to conserve keystrokes. A temporary file is still created then removed; the -i option only saves the user from having to specify it. Maybe the intent is it is only for "one-off" use, not for use in scripts.

This will work for GNU, BSD and Plan9:

   sed -n 's/old/new/wfile.tmp' file
   mv file.tmp file
Or just use redirection.

Given the choice between avoiding some keypresses and more portable scripts, I will keep choosing the later.

NetBSD sed may have the -i option now but I do not see anyone using it in scripts meant to be portable, like build.sh^1

1. https://ftp.netbsd.org/pub/NetBSD/NetBSD-release-9/src/build...

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

#35
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` without the hassle of changing the ownership back.

I never see `lndir` on these lists either. It makes a copy of a directory, but all of the non-directory files in the target are replaced with symlinks back to the source while directories are preserved as-is. Meaning that when you `cd` into it, you are actually landing in a copied structure of the source directory instead of the source directory itself, as would be the case if you just symlinked the source folder.

Once inside, any file you want to modify without affecting the original just needs you to create the symlink into a file, which you can do with `sed -i '' $symlink`. There you have it: effectively a copy of your original directory, with only the modified files actually taking up space (loosely speaking).

Looks like I have a few pull requests to submit.

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

#36
post #23
post #21

Earlier quoted context omitted.

I don't recall where I heard it, but my understanding is that socat is the sort of successor to good ol' netcat. (Of course, don't ask me to compare each, nor know what socat brings that netcat lacks, etc.)

`socat` is netcat but for serial. [0] [0] https://serverfault.com/questions/246347/whats-the-differenc...

Oooohhh!

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

#37

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`…

FWIW `realpath` on macOS should be functionally equivalent to `readlink -f` - particularly if you ignore all the other functionality `readlink` provides.

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

#38
post #17
post #4

Earlier 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…

It's simpler to use a tmpfs for this purpose. $XDG_RUNTIME_DIR is already available, on modern Linux versions.

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

#39
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 `#`

alt-# is quite enough.

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

#40

Earlier quoted context omitted.

> Watch out, that's a Linux-ism and macOS's sed It's GNU sed vs (Free)BSD sed, which are different enhancements of the POSIX standards for sed that went in different design directions. One could Homebrew/macports install gnu-sed on macOS to get a GNU version to write Linux-portable scripts as-needed.

Plan9 sed has no -i option. Older versions of NetBSD will not have it either. I never understood the point of the -i option other than to conserve keystrokes. A temporary file is still created then removed; the -i option only saves the user from having to specify it. Maybe the intent is it is only for "one-off" use, not for use in scripts. This will work for GNU, BSD and Plan9: sed -n 's/old/new/wfile.tmp' file mv fi…

Yeah, I've heard that argument, too, but I fear that ship has sailed

Also, be aware you currently have duplicated comments: https://news.ycombinator.com/item?id=31254181

Post reply on HN