1

I was once shown a shortcut that I can't recall/ find. After pinging an ip, then attempting to ssh into it - the ip was saved into to a variable automagically.

Something like:

    ping 10.1.23.4
    ...
    ssh root@$!

and the ip was replicated in the second line replacing $! without further ado.

So does this ring a bell as to what the actual syntax is? Or was this some special configuration? I believe this was running centos if that is related. tia

2 Answers2

3

This is a bashism -- bash being the default shell language in CentOS. (Csh has something similar.) Check out the part about History Expansion in the manual; probably you are interested in !*, which is "all the arguments to the previous command", or !^, which will give you the first one, or !$, which will give you the last.

Note that these aren't variables. They're expanded and replaced with the replacement value in the history.

mattdm
  • 40,245
  • Or better yet; ssh root@!$... – jasonwryan 6 mins ago -> in comment above – user1541542 Aug 13 '13 at 04:07
  • Yes, although I'm mildly disturbed that you have a system into which you can ssh as root. :) – mattdm Aug 13 '13 at 04:14
  • bash (and zsh) copied it from csh (which invented it over 10 years earlier), so I'd rather call it a cshism. POSIX shells have $_ for that. – Stéphane Chazelas Aug 13 '13 at 05:48
  • @StephaneChazelas Well, except the shell in question is bash. I guess we could call it a csh-ism that bash borrowed — the bash documentation readily says so. – mattdm Aug 13 '13 at 12:21
0

Another alternative, if you are using bash in emacs editing mode (the default), then you can do:

$ ping x.x.x.xCtrl-wCtrl-y

The Ctrl-w will delete the last word (the IP address), putting it in the kill ring, Ctrl-y will paste the contents of the kill ring.

You can then run any number of commands, and then at the point you need the IP again just reuse Ctrl-y

$ ssh root@Ctrl-y

Note that issuing any kill related functions on a new command line, will replace the contents of the kill ring:

$ ping x.x.x.xCtrl-wCtrl-y

$ touch /tmp/example.fileCtrl-wCtrl-y

$ ssh root@Ctrl-y

will result in:

$ ssh root@/tmp/example.file

Drav Sloan
  • 14,345
  • 4
  • 45
  • 43