Live data from Hacker News

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

github.com

71–80 of 114 posts

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

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

This is useful when saving command lines to files (scripts) using the POSIX-required fc builtin. Command line histories are relatively cumbersome to save with Ash, Bash saves them but truncates them to 500 entries, whereas scripts can easily be saved indefinitely. Amongst Bash and other feature-heavy shell users, there are Rube Goldberg-like workarounds for command line history saving. OTOH, all shells aiming for POS…

> Bash saves them but truncates them to 500 entries

That behavior can be modified with the HISTSIZE environment variable.

> The maximum number of commands to remember on the history list. If the value is 0, commands are not saved in the history list. Numeric values less than zero result in every command being saved on the history list (there is no limit). The shell sets the default value to 500 after reading any startup files.

https://www.gnu.org/software/bash/manual/html_node/Bash-Vari...

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

#73

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

"And `cp -a`, which is like `cp -r` which leaves permissions intact - meaning you can prepend `sudo` without the hassle of changing ownership back."

I always use pax instead of cp

            mkdir newdir
            cd olddir
            pax -rw -pp . ../newdir
preserves permissions and access times

            cd olddir
            pax -rw -pe . ../newdir
preserves ownership, permissions and access times.

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

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

With Perl, you can't just say "Perl", you have to be specific to which version of Perl.

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

#75

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

For canonicalizing directories, you can use this portably:

  dir=$(cd -P -- "$dir" && pwd)
with the usual gotcha that $() strips trailing newlines.

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

#76

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…

> sed -n 's/old/new/wfile.tmp' file

This saves only lines which underwent substitution, which was probably not what you wanted.

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

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

With Perl, you can't just say "Perl", you have to be specific to which version of Perl.

What do you mean?

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

#78
post #77

Earlier quoted context omitted.

With Perl, you can't just say "Perl", you have to be specific to which version of Perl.

What do you mean?

How many major versions of Perl do you know about?

What is the distribution of these different versions of Perl across various OSes and OS versions?

Hint: Lots of backward-incompatible changes tend to get made around different major versions. Having Perl 4 is not like having Perl 5 which is not like Perl 6.

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

#79
post #25

New users on my systems commonly ask me "what implements your pps process search?" When the shell itself filters the output of ps, then removing a grep is unnecessary. Note this uses POSIX shell patterns, not regular expressions. On a truly POSIX shell that does not support "local," remove the keyword, and change the braces to parentheses to force the function into a subshell. pps () { local a= b= c= IFS='\0'; ps ax…

IFS='\0' doesn't do what it seems: https://github.com/koalaman/shellcheck/wiki/SC2141

You almost always want "read -r": https://github.com/koalaman/shellcheck/wiki/SC2162

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

#80
post #76

Earlier quoted context omitted.

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…

> sed -n 's/old/new/wfile.tmp' file This saves only lines which underwent substitution, which was probably not what you wanted.

Missing semicolon.

  sed -n 's/old/new/;w file.tmp' file
Unlike BSD and GNU sed, it appears that Plan9 sed will append to instead of overwite file.tmp

It also requires a space after the w command.

Post reply on HN