55

I want to create a shortcut for a command in terminal. Like for a command "ssh user@123.45.7.123", I just want a command "user" and the above command will run. Is this possible, and if so how can it be done?

Thank you!

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
rohit
  • 661

4 Answers4

74

The shortcuts for commands are known as aliases.
The syntax to create an alias is:

alias custom_command='original_command'  

Example:
For creating an alias for update and upgrade we should type the following in terminal,

alias update='sudo apt-get update'    

alias upgrade='sudo apt-get upgrade'  

So to create an alias for your command open the termianl and type:

alias user='ssh user@123.45.7.123'
M.Tarun
  • 5,121
24

If you want to not load the alias every time, to permanently store a alias command, do this.

Go to your home directory and press Ctrl+H to view hidden files, one of these files would be .bashrc. Open it.

Now write the following command anywhere in the mainspace:

alias custom_command='original_command' 

Your shortcut command will be stored permanently.

andrew.46
  • 39,359
8

Aliases can take parameters. For example:

$ alias 777='sudo chmod -R 777 '
$ 777 MyFolder

will perform chmod recursively on MyFolder

4

Generally the answer is to alias your command, as mentioned by M.Tarun. For your example with ssh you might want to add it to your .ssh/config:

Host someName
     HostName 123.45.7.123
     User user

Then call ssh with the name:

$ ssh someName

Your shell probably also has tab-completion for ssh. So you can just type ssh s and then hit Tab.

This also has the advantage that it works with other commands like scp:

$ scp some-file someName:a/path/

Whereas the alias approach would not work with this.

ahilsend
  • 1,540