50

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
  • 169,590
rohit
  • 611
  • 3
  • 7
  • 6

4 Answers4

69

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,001
  • 6
  • 33
  • 64
  • 11
    If you want to have this command available in every terminal, you need to add this line to ~/.bash_aliases. Then it will be automatically loaded. – panmari Aug 03 '13 at 09:47
  • i dont have any ~/.bash_aliases file should i create it? – rohit Aug 03 '13 at 09:55
  • 3
    That depends on your ~/.bashrc . Does it source ~/.bash_aliases ? If yes, then just create it. – ahilsend Aug 03 '13 at 09:58
  • 5
    @rohit the .bash_aliases file is not a standard bash feature (I think it is an Ubuntu thing), the standard way is to add alias definitions to your .bashrc file. – terdon Aug 03 '13 at 15:17
  • if you added your alias to ~/.bash_aliases or ~/.bashrc don't forget to activate it by source ~/.bash_aliases or source ~/.bashrc – Mahdi mehrabi Aug 16 '20 at 05:59
  • The changes suggested above didn't work right away until I had restarted the terminal. – carloswm85 May 14 '21 at 19:49
23

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
  • 38,003
  • 27
  • 156
  • 232
  • 1
    Apologies if this is a dumb question, but what is meant by "mainspace"? Where, if anywhere, would be the "safest" location for a beginner to insert this text? – Baku9 Jan 28 '20 at 05:27
  • 1
    Safest "Mainspace" would be the ones not indented and are not inside any scope (methods, brackets, etc.). – Kartik Raj Feb 11 '20 at 19:02
  • 1
    you need source .bashrc after closing .bashrc with ctrl-x and saying yes to save changes – mLstudent33 Apr 27 '20 at 01:13
7

Aliases can take parameters. For example:

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

will perform chmod recursively on MyFolder

  • Do you know how I can use alias for a command like this to trim a video?

    ffmpeg -ss 00:00:00 -i input.mp4 -to 00:00:15 -c copy output.mp4

    So that I can just type in the values that change, like the time and the filenames

    – Harsha Mar 16 '20 at 08:42
3

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,480
  • 2
  • 10
  • 10