read bash history via ssh does not work

This does work:

server:~> history | tail -100   | grep etc
  908  2018-01-17 08:53:43 cd /etc/apache2/vhosts.d/default.d/
  ....
  ....

But this does not:

ssh root@server "history | tail -100 | grep etc"

Why?


Apparently bash does not enable history handling when run non-interactively.

You can work around this by starting bash with the -i for interactive option:

ssh root@server "echo history | bash -i | tail -100 | grep etc"