I want to create the alias for the command "ssh user@10.32.44.225" as 10.32.44.225
alias [0-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9]="ssh user@$0"
I have created it, but it is not working. Is there any syntax error in this command?
Best Answer
This is doable - even with
alias
, though the only way I know would involveeval
which can be dangerous depending on what the arguments might contain - but not with regex. The real problem here is tokenization - for each simple command the shell reads in it attempts to execute whatever word it finds in command position as an alias first, then. failing that, as a function/builtin, and, failing both of those, as a$PATH
ed executable. This is a simplification of the process, but it is pretty close to the truth.The problem is that you have in a single word two possible tokens -
ssh
is what you'd like to execute, and@$IP
is an argument with which you'd like to execute it. You can do as has been elsewhere suggested and simply split the token in two combined with a shell function - this is simply done. Or you can get the first token to split itself.An interesting thing about shell
$var
names is they are pretty explicit about what characters they can contain. Save a single special case,@
is not among those. If I do:The shell will interpret the entire string as a single variable name because all of the characters within are allowable within a
$var
name. But......results in two tokens (if not necessarily two words) and the shell expands
$var
independently of @the_rest. Alias expansion, unfortunately, has no such convenient marker as$
and so the shell will always attempt to expand either......as a single token. It is also because the shell expands both
$var
andaliased cmdstring
simultaneously that the latter cannot be contained within the former and still be executed withouteval
- but then any arguments given$var
- even${varcontains='$(cmd substitutions)'}
will be evaluated twice - which can be fairly dangerous.So I propose you do something with a
readonly
variable named$ssh
that looks like this:That way you can do...
...or...
...and etc. or whatever, because the shell will split out the two tokens which are
$ssh
and@somplace.com
into two words. It does not work, however, if you......because in that case, though you get two tokens, the result is still single word. I used
readonly
above to ensure that the$ssh
varname cannot be sabotaged and reset to something more dangerous, but this might still be possible to do with thessh
function for any normal user in the shell. I'm unaware of any means that can be used toreadonly
a shell function, but, if this is a concern for you, then you may want to locate the function in a$PATH
ed shell script with appropriate read/write rights.Then again, it can work without a function at all if set from a profile file or whatever:
But it is less flexible that way.