@mitch (This works for single commands. Not sure what is in your one-liners.)

Follow

@mitch here's an alias for your `.bashrc`:

`alias resudo="(export \$(compgen -v); fc -ln -- | grep -Pv '^\s*resudo(?![^\s])' | tail -n 1 | envsubst | sudo su)"`

This handles one-liners with pipes, semicolons, ampersands, etc., which the double-bang version can't. It's slightly different from your request in that it executes the whole one-liner with a single privilege escalation rather than one for each constituent command, which no doubt will prove important in some edge case.

Briefly explained, the core idea is to pipe `fc` to `su`. The `export`, `compgen`, and `envsubst` parts are there to ensure that any environment variables get expanded. The `grep` and `tail` are there to exclude itself from the definition of "last-used one-liner", in case you should want to repeatedly invoke it. The whole thing is in a subshell to insulate you from the side effects of `export`.

@aarbrk

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.