Live data from Hacker News

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

github.com

101–110 of 114 posts

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

#101
post #57
post #53

Earlier quoted context omitted.

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

Yeah, I actually thought about that afterward, however, in that same "I wonder what happened", I also wondered if tput is bright enough to know the difference between the local termcap and the connected one 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 t…

Your example suffers from being a toy example; it makes no sense to run a noninteractive command in a docker container merely to output terminal escape codes. If this were the norm, "docker run” would probably by default make sure to copy the TERM setting to the inner command.

I would assume that if you run an interactive shell inside docker, TERM would actually be set correctly. It’s the same when you ssh somewhere else – the TERM environment variable is sent along, so that the remote program can see it and output the correct codes for your local terminal. Also, the docker image needs the terminfo database installed for “tput” to work.

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

#102

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.

kitty can do this with shell integration https://sw.kovidgoyal.net/kitty/shell-integration/

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

#103
post #33

Earlier quoted context omitted.

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

Huh, interesting.

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

#104
post #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

Thanks for the critique.

  pps () { local a= b= c= IFS=$'\0'; ps ax | while read -r a
    do [ "$b" ] || c=1; for b; do case "$a" in *"$b"*) c=1;;
      esac; done; [ "$c" ] && printf '%s\n' "$a" && c=; done; }

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

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

Is that Ctrl+x, Ctrl+e? Or Ctrl+x+e? Or both controls?

it's c-x followed by c-e although no need to release the control key that I'm aware of. The c-x is in the same family as alt-x (sometimes called "meta-x") which is a similar Emacs "mode switching" leader keystroke

You can read about the gory details in "man 3 readline": https://manpages.ubuntu.com/manpages/jammy/en/man3/readline....

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

#106

This is a rather long list so i'll just mention my latest favorite which I recently learned on HN and it has made my life very easy with bash Whenever 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

I know that may be convenient, but you'll want to exercise caution because $'' turns on inner escaping that wouldn't otherwise happen inside single-quoted strings

    $ echo 'hello \"world'
    hello \"world
    $ echo $'hello \"world'
    hello "world

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

#107

Earlier quoted context omitted.

might be kludgey but i wonder if bash coproc would help, it basically can send stdout to a named pipe that makes it avail as a environment variable fike descriptor.

Since bash can only handle one coproc at a time, using it behind the scenes blocks that function interactively.

Oops, that’s not true for v5. Sorry

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

#108

  Ctrl + s : to stop output to terminal.
  Ctrl + q : to resume output to terminal after Ctrl + s.
Who uses these? These are newb as well as advanced user killers that doesn't seem to serve a purpose.

It just makes you think your shell is locked up making you think either the server is out of memory, process is hung to the point no keys would react or the network is hosed.

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

#109
post #82

Earlier quoted context omitted.

I still don't know what your point is. Are there any contemporary distros that have the "perl" exectuable that is not Perl 5?

If you want to claim broad compatibility, you can't just look at the latest distributions. You have to look at OSes other than Linux. You have to look at older versions of OSes, too. And don't forget the billions of embedded and handheld devices, too.

> You have to look at older versions of OSes, too.

Perl 5 was released in 1994. It is incredibly portable. If you have a system that legitimately has Perl 4, I would love to hear about it, but …

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

#110
post #71

Earlier quoted context omitted.

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

Bash is too complicated. Too many options. I am not smart enough to use it. I prefer the Almquist shell. No shellshock. echo "export PROMPT_COMMAND='history -a;history -r;export HISTFILE=-1;export HISTFILESIZE=-1;shopt -s histappend'" > .bashrc cp .bashrc /etc/profile

When I peruse bash-5.1/CHANGES it makes me uncomfortable. It is far too long, for one.
Post reply on HN