0

This is a theoretical mind game bugging me.

If I would like to prepend commands like sudo and so on to commands being entered, an approach would be to set up aliases for specific commands. But I was wondering if also a generic approach is possible, so now matter what I enter as command a specific command/string will be added to beginning of the command line.

Any ideas?

2
  • alias foo='sudo foo' will run sudo foo when you enter foo. It's a really bad idea because you can very easily break your system or irretrievably lose your data that way (I don't expect people to have backups anymore) but that's how you'd do it.
    – l0b0
    Mar 15, 2021 at 21:25
  • Cheers. That's the approach I already came up with, but it is a bit tedious if the list of commands is pretty long. The prepended command would not be used for actions tampering with the system. Sudo was maybe a bad example
    – wakkaluba
    Mar 16, 2021 at 7:44

1 Answer 1

-1

You might be interested in the !! history substitution, which substitutes !! with your last command.

$ apt update
Reading package lists... Done                                                                                           
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)                                      
E: Unable to lock directory /var/lib/apt/lists/                                                                         
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)
$ sudo !!
[sudo] password for panki:
[...]
Reading package lists... Done
Building dependency tree
Reading state information... Done
42 packages can be upgraded. Run 'apt list --upgradable' to see them.

If this happens to you often, you might want to consider setting up an alias:

alias dangit='sudo !!'

Which makes this possible:

$ less /etc/shadow                                                                                                                                                                                       
/etc/shadow: Permission denied
$ dangit
<less opens the file>
1
  • Not exactrly what I was looking for, but this is a cool trick I will keep in mind :)
    – wakkaluba
    Mar 15, 2021 at 8:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .