41

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!

0
58

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'
5
  • 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 3 '13 at 9:47
  • i dont have any ~/.bash_aliases file should i create it? – rohit Aug 3 '13 at 9:55
  • 3
    That depends on your ~/.bashrc . Does it source ~/.bash_aliases ? If yes, then just create it. – ahilsend Aug 3 '13 at 9: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 3 '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 5:59
21

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.

3
  • 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 5: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 1:13
7

Aliases can take parameters. For example:

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

will perform chmod recursively on MyFolder

1
  • 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 8:42
1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.