What does !^ in csh alias do

Note: my shell is csh

An ex-co-worker gave me the following command to put in my .cshrc:

alias bastion 'ssh -L !^:<some_ip_address> <username>@<something.com>'

Note that what is in between <> are actual values.

Now when I run this on my shell, I get the following error: Bad ! arg selector.

I’ve been searching the web for similar uses of ssh -L but I haven’t found an example that uses this syntax of using the !^ in front of the :.

Any idea what this command is actually trying to do and why it’s not working?

Asked By: Simon


Not a csh user myself but this seem to be history control.

!^ gives the first argument of the previous command. This is the same as !:1 . Remember that, just as the ^ (caret) is the beginning-of-line anchor in regular expressions ( 26.4 ) , !^ gives the beginning history argument.

% cat fn fn1 fn2
% more !^
more fn


The (I assume) are escapes to prevent the !^ from being interpreted early (i.e. not when the alias is defined but when it’s used). See examples here.

So this basically implements argument handling for your alias. The alias is intended to be called with a local port number to bind to¹.

$ bastion 8080 
# should be executed as
$ ssh -L 8080:<some_ip_address> <username>@<something.com>

$ bastion 8080 some argument
# is probably (I'm still no csh user) executed as
$ ssh -L 8080:<some_ip_address> <username>@<something.com> some argument

As conclusion I assume Bad ! arg selector. means there where no arg to select because you tried to run it as bastion without port argument.

¹ I assume your replacement <some_ip_address> was actually an IP + port? Otherwise the -L argument would be a bit strange.

Answered By: Paul Pazderski
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.