Search code examples
linuxshellaliascsh

Converting a rlogin command into an alias


I work on multiple machines and need to login into them remotely. I want to have an alias which can make my rlogin command simpler.

So, I want to convert following command into an alias :

rlogin `echo "machine $num" | tr -d ' '`

I tried writing this in my .cshrc file :

alias rl rlogin `echo machine$1| tr -d ' '`

when I do rl 13

It says :

usage: rlogin [ -8EL] [-e char] [ -l username ] host

What am I missing here ?


Solution

  • You can easily see the alias is not defined as intended, by running alias rl:

    % alias rl rlogin `echo machine$1| tr -d ' '`
    % alias rl
    rlogin machine
    

    There are two problems with the way you define your alias.

    First, when defining an alias like this:

    alias rl rlogin `...`
    

    the ... command is evaluated at the time the alias is defined, while you need to defer the evaluation to the time it is used. The correct way to do it is to encapsulate everything in single quotes, like

    '`...`'
    

    (Also, we need to replace the internal single-quotes with double-quotes, in order not to clash with the outer single-quotes).

    Second, you need to use \!* instead of $1.

    So you get:

    % alias rl rlogin '`echo machine\!* | tr -d " "`'
    % alias rl
    rlogin `echo machine!* | tr -d " "`