3

Any time I run ssh (or scp or lftp), I'd like to run gpg-connect-agent updatestartuptty /bye first. (It's a long story.)

Currently, I have a function defined in my .bashrc:

ssh()
{
  gpg-connect-agent updatestartuptty /bye >/dev/null 2>&1
  $(which ssh) "$@"
}

This works just fine, but I'm imagining there could potentially be portability issues if used on another system where ssh is already aliased similarly for something else.

Is there any way to define a pre-execution "callback" on ssh without overriding the ssh command itself? (I'm aware that you can use trap DEBUG to run a given command prior to EVERY command, but I'd like to limit this to just the commands I need it on.)

3
  • It would be just as unportable as you'd again have to setup it. Another option would be to make ssh a symlink to your script but you're probably going to run into all kinds of issues with that. But you would be independent in a way. – Seth Jul 7 '17 at 8:03
  • Not sure I understand your question, but if you run \ssh you are overriding any alias set and using the original command. So what you could do is 1. call your function differently so that it doesn't overlap with anything 2. change the ssh call in the function to \ssh – Anon Jul 7 '17 at 8:26
  • @Seth: I'm asking if bash has any sort of built-in pre-command-execution callback mechanism — the idea being that setting a callback would be less intrusive than simply overriding a command with a function of the same name, and wouldn't require a separate script file. @Anon: I find that prepending with `` overrides aliases but not functions. – Ryan Lue Jul 7 '17 at 9:56
1

I suggest to write like this instead:

ssh()
{
  gpg-connect-agent updatestartuptty /bye &>/dev/null
  command ssh "$@"
}

See help command for more details.

And no, there's no other "pre-callback mechanism". If there is, what you're doing is basically "it".

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.