0

I log into a lot of servers via ssh. Typically I just use the easy bash history search to scroll through my history of 'ssh' commands and find the one I want. However, eventually my .bash_history reaches its limit and I start losing entries, despite increasing the limits, etc..

I would rather just adjust my $PROMPT_COMMAND so that after every command, it checks to see if I ran a command with ssh, and if so, appends that command to a file somewhere.

I saw some relevant questions asked here and here but I am struggling to understand how to use these in a function that I can source from my .bashrc and add to my $PROMPT_COMMAND that will check if the last command entered started with ssh and copy it to a file.

For example, this does not work:

$ PROMPT_COMMAND="echo; foo; "
$ foo () { echo "command was: $BASH_COMMAND" ; }
$ ssh cn-0030
...(Ctrl-D)...
$ logout
Connection to cn-0030 closed.

command was: echo "command was: $BASH_COMMAND"

This also does not work:

$ foo () { echo !! | grep ssh --color ; }

Because !! gets expanded into the actual last command run immediately and then saved into foo, instead of being evaluated when foo is evaluated

user5359531
  • 3,217
  • 6
  • 30
  • 55
  • but why you don't store all your ssh in .ssh directory config file, isn't better? – mohammad zein Dec 18 '18 at 20:31
  • You might want to spend some time perusing `man ssh_config`, especially the `Host`, `User`, and `RemoteCommand` keywords. You may not need to use shell history as much as you think. – chepner Dec 18 '18 at 20:37
  • Another thought is use another command to ssh (I called mine `go2`) then write that script to enable any smarts you want like auto-completion, history, etc .... – tk421 Dec 18 '18 at 20:47
  • You also might consider adding `history -w` to `foo()` which will save your commands to the .bash_history. – tk421 Dec 18 '18 at 20:53

0 Answers0