0

How can I configure Ubuntu Server so that every time I execute any terminal command, it prepends a specific command I name to that command?

So that:

$ cmd2

is actually received by the system as:

$ cmd1 cmd2

and for any

$ cmd-N

it will be received as

$ cmd1 cmd-N

I want to do this so I can automatically force every command to run under a time limit as described in this post: How could I set a universal time limit on any command line operation?. I don’t want to manually prepend every command with the command; I want it to become an automatic feature of the entire environment.

3
  • 1
    "Every command" is a bit vague. The commands you type in the terminal are of various types. Some are built in, some are binary executables, some are scripts of different types, etc. What may work for one kind of command may not work for another.
    – user68186
    Oct 23, 2021 at 0:22
  • 1
    This smells like an XY problem. You want to achieve X, but you ask about problem Y which you think will solve problem X. Indicate in your question what you actually want to achieve, and good approaches will pop up, different perhaps than what you think will be the approach.
    – vanadium
    Oct 23, 2021 at 7:33
  • superuser.com/questions/117227/…
    – john-jones
    Oct 23, 2021 at 11:33

1 Answer 1

0

You don't give much info on what you want to achieve, but I think that the alias builtin is what you want.

alias cmd2='cmd1 cmd2' does literally what you asked, i.e. alias less='sudo less' will make all subsequent less calls to be translated to sudo less.

To make the alias persistent you have to add the command in your ~/.bashrc file.

You can check out https://opensource.com/article/19/7/bash-aliases for more info and examples.

2
  • Thanks but I updated the question to make it clearer - I meant every command automatically gets CMD-1 prepended to it. Maybe a line in the .bashrc? Oct 23, 2021 at 11:22
  • I would create a short alias and prepend any command, just to be able to omit the "cmd1" if I need/want. If you absolutely want to prepend every line with "cmd1", you can try the link posted by @john-jones.
    – Dion
    Oct 24, 2021 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.