LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   alias has unwanted space in it (https://www.linuxquestions.org/questions/linux-newbie-8/alias-has-unwanted-space-in-it-4175515954/)

CommanderRikker 08-22-2014 11:03 AM

alias has unwanted space in it
 
Hi gang:

I'm trying to create an alias that will copy my public key to other servers. I created this alias in .bashrc:
Code:

alias copykey='ssh-copy-id root@$1'
However, when I type this at a command line:
Code:

copykey server1
I get:
Code:

ssh-copy-id root@ server1
How do I get rid of that space before the server name?

Thanks,

Rick

thesnow 08-22-2014 11:26 AM

Try a function instead (in your .bashrc)

Code:

function copykey { ssh-copy-id root@"$1"; }
ouputs:
Code:

user@lm ~ $ copykey server1
+ copykey server1
+ ssh-copy-id root@server1


grail 08-22-2014 11:35 PM

thesnow is correct, but I thought I would provide the explanation for your issue.

Aliases do not accept parameters and even if they did you would not get the desired result. You need to think of the alias as a cut and paste.
So whatever you assign to your alias will directly replace the name of the alias. So if you could accept parameters, your output would have looked like:
Code:

$ alias copykey='ssh-copy-id root@$1'
$ copykey server1
$ ssh-copy-id root@server1 server1



All times are GMT -5. The time now is 07:28 AM.